home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / doc / python-configobj / configobj.html < prev    next >
Encoding:
Extensible Markup Language  |  2008-06-28  |  189.2 KB  |  2,480 lines

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
  7. <title>Reading and Writing Config Files</title>
  8. <meta name="authors" content="Michael Foord  Nicola Larosa" />
  9. <meta name="date" content="2008/06/27" />
  10. <meta content="ConfigObj - a Python module for easy reading and writing of config files." name="description" />
  11. <meta content="python, script, module, config, configuration, data, persistence, developer, configparser" name="keywords" />
  12. <link rel="stylesheet" href="stylesheets/voidspace_docutils.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="reading-and-writing-config-files">
  16. <h1 class="title">Reading and Writing Config Files</h1>
  17. <h2 class="subtitle" id="configobj-4-introduction-and-reference">ConfigObj 4 Introduction and Reference</h2>
  18. <table class="docinfo" frame="void" rules="none">
  19. <col class="docinfo-name" />
  20. <col class="docinfo-content" />
  21. <tbody valign="top">
  22. <tr><th class="docinfo-name">Authors:</th>
  23. <td>Michael Foord
  24. <br />Nicola Larosa</td></tr>
  25. <tr><th class="docinfo-name">Version:</th>
  26. <td>ConfigObj 4.5.3</td></tr>
  27. <tr><th class="docinfo-name">Date:</th>
  28. <td>2008/06/27</td></tr>
  29. <tr class="field"><th class="docinfo-name">Homepage:</th><td class="field-body"><a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj Homepage</a></td>
  30. </tr>
  31. <tr class="field"><th class="docinfo-name">Sourceforge:</th><td class="field-body"><a class="reference external" href="http://sourceforge.net/projects/configobj">Sourceforge</a></td>
  32. </tr>
  33. <tr class="field"><th class="docinfo-name">Development:</th><td class="field-body"><a class="reference external" href="http://svn.pythonutils.python-hosting.com">SVN Repository</a></td>
  34. </tr>
  35. <tr class="field"><th class="docinfo-name">License:</th><td class="field-body"><a class="reference external" href="http://www.voidspace.org.uk/python/license.shtml">BSD License</a></td>
  36. </tr>
  37. <tr class="field"><th class="docinfo-name">Support:</th><td class="field-body"><a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a></td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <div class="contents topic" id="configobj-manual">
  42. <p class="topic-title first">ConfigObj Manual</p>
  43. <ul class="auto-toc simple">
  44. <li><a class="reference internal" href="#introduction" id="id28">1   Introduction</a></li>
  45. <li><a class="reference internal" href="#downloading" id="id29">2   Downloading</a><ul class="auto-toc">
  46. <li><a class="reference internal" href="#files" id="id30">2.1   Files</a></li>
  47. <li><a class="reference internal" href="#documentation" id="id31">2.2   Documentation</a></li>
  48. <li><a class="reference internal" href="#pythonutils" id="id32">2.3   Pythonutils</a></li>
  49. <li><a class="reference internal" href="#development-version" id="id33">2.4   Development Version</a></li>
  50. </ul>
  51. </li>
  52. <li><a class="reference internal" href="#configobj-in-the-real-world" id="id34">3   ConfigObj in the Real World</a></li>
  53. <li><a class="reference internal" href="#getting-started" id="id35">4   Getting Started</a><ul class="auto-toc">
  54. <li><a class="reference internal" href="#reading-a-config-file" id="id36">4.1   Reading a Config File</a></li>
  55. <li><a class="reference internal" href="#writing-a-config-file" id="id37">4.2   Writing a Config File</a></li>
  56. <li><a class="reference internal" href="#config-files" id="id38">4.3   Config Files</a></li>
  57. </ul>
  58. </li>
  59. <li><a class="reference internal" href="#configobj-specifications" id="id39">5   ConfigObj specifications</a><ul class="auto-toc">
  60. <li><a class="reference internal" href="#infile" id="id40">5.1   infile</a></li>
  61. <li><a class="reference internal" href="#options" id="id41">5.2   options</a></li>
  62. <li><a class="reference internal" href="#methods" id="id42">5.3   Methods</a><ul class="auto-toc">
  63. <li><a class="reference internal" href="#write" id="id43">5.3.1   write</a></li>
  64. <li><a class="reference internal" href="#validate" id="id44">5.3.2   validate</a><ul class="auto-toc">
  65. <li><a class="reference internal" href="#return-value" id="id45">5.3.2.1   Return Value</a></li>
  66. <li><a class="reference internal" href="#mentioning-default-values" id="id46">5.3.2.2   Mentioning Default Values</a></li>
  67. <li><a class="reference internal" href="#mentioning-repeated-sections" id="id47">5.3.2.3   Mentioning Repeated Sections</a></li>
  68. <li><a class="reference internal" href="#mentioning-simpleval" id="id48">5.3.2.4   Mentioning SimpleVal</a></li>
  69. <li><a class="reference internal" href="#mentioning-copy-mode" id="id49">5.3.2.5   Mentioning copy Mode</a></li>
  70. </ul>
  71. </li>
  72. <li><a class="reference internal" href="#reload" id="id50">5.3.3   reload</a></li>
  73. <li><a class="reference internal" href="#reset" id="id51">5.3.4   reset</a></li>
  74. </ul>
  75. </li>
  76. <li><a class="reference internal" href="#attributes" id="id52">5.4   Attributes</a><ul class="auto-toc">
  77. <li><a class="reference internal" href="#interpolation" id="id53">5.4.1   interpolation</a></li>
  78. <li><a class="reference internal" href="#stringify" id="id54">5.4.2   stringify</a></li>
  79. <li><a class="reference internal" href="#bom" id="id55">5.4.3   BOM</a></li>
  80. <li><a class="reference internal" href="#initial-comment" id="id56">5.4.4   initial_comment</a></li>
  81. <li><a class="reference internal" href="#final-comment" id="id57">5.4.5   final_comment</a></li>
  82. <li><a class="reference internal" href="#list-values" id="id58">5.4.6   list_values</a></li>
  83. <li><a class="reference internal" href="#encoding" id="id59">5.4.7   encoding</a></li>
  84. <li><a class="reference internal" href="#default-encoding" id="id60">5.4.8   default_encoding</a></li>
  85. <li><a class="reference internal" href="#unrepr" id="id61">5.4.9   unrepr</a></li>
  86. <li><a class="reference internal" href="#write-empty-values" id="id62">5.4.10   write_empty_values</a></li>
  87. <li><a class="reference internal" href="#newlines" id="id63">5.4.11   newlines</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </li>
  92. <li><a class="reference internal" href="#the-config-file-format" id="id64">6   The Config File Format</a></li>
  93. <li><a class="reference internal" href="#sections" id="id65">7   Sections</a><ul class="auto-toc">
  94. <li><a class="reference internal" href="#section-attributes" id="id66">7.1   Section Attributes</a></li>
  95. <li><a class="reference internal" href="#section-methods" id="id67">7.2   Section Methods</a></li>
  96. <li><a class="reference internal" href="#walking-a-section" id="id68">7.3   Walking a Section</a></li>
  97. <li><a class="reference internal" href="#examples" id="id69">7.4   Examples</a></li>
  98. </ul>
  99. </li>
  100. <li><a class="reference internal" href="#exceptions" id="id70">8   Exceptions</a></li>
  101. <li><a class="reference internal" href="#validation" id="id71">9   Validation</a><ul class="auto-toc">
  102. <li><a class="reference internal" href="#configspec" id="id72">9.1   configspec</a></li>
  103. <li><a class="reference internal" href="#type-conversion" id="id73">9.2   Type Conversion</a></li>
  104. <li><a class="reference internal" href="#default-values" id="id74">9.3   Default Values</a><ul class="auto-toc">
  105. <li><a class="reference internal" href="#id15" id="id75">9.3.1   List Values</a></li>
  106. </ul>
  107. </li>
  108. <li><a class="reference internal" href="#repeated-sections" id="id76">9.4   Repeated Sections</a></li>
  109. <li><a class="reference internal" href="#copy-mode" id="id77">9.5   Copy Mode</a></li>
  110. <li><a class="reference internal" href="#validation-and-interpolation" id="id78">9.6   Validation and Interpolation</a></li>
  111. <li><a class="reference internal" href="#simpleval" id="id79">9.7   SimpleVal</a></li>
  112. </ul>
  113. </li>
  114. <li><a class="reference internal" href="#empty-values" id="id80">10   Empty values</a></li>
  115. <li><a class="reference internal" href="#unrepr-mode" id="id81">11   unrepr mode</a></li>
  116. <li><a class="reference internal" href="#string-interpolation" id="id82">12   String Interpolation</a></li>
  117. <li><a class="reference internal" href="#comments" id="id83">13   Comments</a></li>
  118. <li><a class="reference internal" href="#flatten-errors" id="id84">14   flatten_errors</a><ul class="auto-toc">
  119. <li><a class="reference internal" href="#example-usage" id="id85">14.1   Example Usage</a></li>
  120. </ul>
  121. </li>
  122. <li><a class="reference internal" href="#configobj-3" id="id86">15   ConfigObj 3</a></li>
  123. <li><a class="reference internal" href="#credits" id="id87">16   CREDITS</a></li>
  124. <li><a class="reference internal" href="#license" id="id88">17   LICENSE</a></li>
  125. <li><a class="reference internal" href="#todo" id="id89">18   TODO</a></li>
  126. <li><a class="reference internal" href="#issues" id="id90">19   ISSUES</a></li>
  127. <li><a class="reference internal" href="#changelog" id="id91">20   CHANGELOG</a><ul class="auto-toc">
  128. <li><a class="reference internal" href="#version-4-5-3" id="id92">20.1   2008/06/27 - Version 4.5.3</a></li>
  129. <li><a class="reference internal" href="#version-4-5-2" id="id93">20.2   2008/02/05 - Version 4.5.2</a></li>
  130. <li><a class="reference internal" href="#version-4-5-1" id="id94">20.3   2008/02/05 - Version 4.5.1</a></li>
  131. <li><a class="reference internal" href="#version-4-5-0" id="id95">20.4   2008/02/05 - Version 4.5.0</a></li>
  132. <li><a class="reference internal" href="#version-4-4-0" id="id96">20.5   2007/02/04 - Version 4.4.0</a></li>
  133. <li><a class="reference internal" href="#version-4-3-3-alpha4" id="id97">20.6   2006/12/17 - Version 4.3.3-alpha4</a></li>
  134. <li><a class="reference internal" href="#version-4-3-3-alpha3" id="id98">20.7   2006/12/17 - Version 4.3.3-alpha3</a></li>
  135. <li><a class="reference internal" href="#version-4-3-3-alpha2" id="id99">20.8   2006/12/09 - Version 4.3.3-alpha2</a></li>
  136. <li><a class="reference internal" href="#version-4-3-3-alpha1" id="id100">20.9   2006/12/09 - Version 4.3.3-alpha1</a></li>
  137. <li><a class="reference internal" href="#version-4-3-2" id="id101">20.10   2006/06/04 - Version 4.3.2</a></li>
  138. <li><a class="reference internal" href="#version-4-3-1" id="id102">20.11   2006/04/29 - Version 4.3.1</a></li>
  139. <li><a class="reference internal" href="#version-4-3-0" id="id103">20.12   2006/03/24 - Version 4.3.0</a></li>
  140. <li><a class="reference internal" href="#version-4-2-0" id="id104">20.13   2006/02/16 - Version 4.2.0</a></li>
  141. <li><a class="reference internal" href="#version-4-1-0" id="id105">20.14   2005/12/14 - Version 4.1.0</a></li>
  142. <li><a class="reference internal" href="#version-4-0-2" id="id106">20.15   2005/12/02 - Version 4.0.2</a></li>
  143. <li><a class="reference internal" href="#version-4-0-1" id="id107">20.16   2005/11/05 - Version 4.0.1</a></li>
  144. <li><a class="reference internal" href="#version-4-0-0" id="id108">20.17   2005/10/17 - Version 4.0.0</a></li>
  145. <li><a class="reference internal" href="#version-4-0-0-beta-5" id="id109">20.18   2005/09/09 - Version 4.0.0 beta 5</a></li>
  146. <li><a class="reference internal" href="#version-4-0-0-beta-4" id="id110">20.19   2005/09/07 - Version 4.0.0 beta 4</a></li>
  147. <li><a class="reference internal" href="#version-4-0-0-beta-3" id="id111">20.20   2005/08/28 - Version 4.0.0 beta 3</a></li>
  148. <li><a class="reference internal" href="#version-4-0-0-beta-2" id="id112">20.21   2005/08/25 - Version 4.0.0 beta 2</a></li>
  149. <li><a class="reference internal" href="#version-4-0-0-beta-1" id="id113">20.22   2005/08/21 - Version 4.0.0 beta 1</a></li>
  150. <li><a class="reference internal" href="#version-3-0-0" id="id114">20.23   2004/05/24 - Version 3.0.0</a></li>
  151. <li><a class="reference internal" href="#version-2-0-0-beta" id="id115">20.24   2004/03/14 - Version 2.0.0 beta</a></li>
  152. <li><a class="reference internal" href="#version-1-0-5" id="id116">20.25   2004/01/29 - Version 1.0.5</a></li>
  153. <li><a class="reference internal" href="#origins" id="id117">20.26   Origins</a></li>
  154. </ul>
  155. </li>
  156. <li><a class="reference internal" href="#footnotes" id="id118">21   Footnotes</a></li>
  157. </ul>
  158. </div>
  159. <div class="section" id="introduction">
  160. <h1><a class="toc-backref" href="#id28">1   Introduction</a></h1>
  161. <p><strong>ConfigObj</strong> is a simple but powerful config file reader and writer: an <em>ini
  162. file round tripper</em>. Its main feature is that it is very easy to use, with a
  163. straightforward programmer's interface and a simple syntax for config files.
  164. It has lots of other features though :</p>
  165. <ul>
  166. <li><p class="first">Nested sections (subsections), to any level</p>
  167. </li>
  168. <li><p class="first">List values</p>
  169. </li>
  170. <li><p class="first">Multiple line values</p>
  171. </li>
  172. <li><p class="first">String interpolation (substitution)</p>
  173. </li>
  174. <li><p class="first">Integrated with a powerful validation system</p>
  175. <blockquote>
  176. <ul class="simple">
  177. <li>including automatic type checking/conversion</li>
  178. <li>repeated sections</li>
  179. <li>and allowing default values</li>
  180. </ul>
  181. </blockquote>
  182. </li>
  183. <li><p class="first">When writing out config files, ConfigObj preserves all comments and the order of members and sections</p>
  184. </li>
  185. <li><p class="first">Many useful methods and options for working with configuration files (like the 'reload' method)</p>
  186. </li>
  187. <li><p class="first">Full Unicode support</p>
  188. </li>
  189. </ul>
  190. <p>For support and bug reports please use the ConfigObj <a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a>.</p>
  191. </div>
  192. <div class="section" id="downloading">
  193. <h1><a class="toc-backref" href="#id29">2   Downloading</a></h1>
  194. <p>The current version is <strong>4.5.3</strong>, dated 27th June 2008. ConfigObj 4 is
  195. stable and mature. We still expect to pick up a few bugs along the way though <a class="footnote-reference" href="#id16" id="id1">[1]</a>.
  196.  <img src="/smilies/smile.gif" alt="Smile" height="15" width="15" /> </p>
  197. <p>You can get ConfigObj in the following ways :</p>
  198. <div class="section" id="files">
  199. <h2><a class="toc-backref" href="#id30">2.1   Files</a></h2>
  200. <ul>
  201. <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj.py">configobj.py</a> from Voidspace</p>
  202. <blockquote>
  203. <p>ConfigObj has no external dependencies. This file is sufficient to access
  204. all the functionality except <a class="reference internal" href="#validation">Validation</a>.</p>
  205. </blockquote>
  206. </li>
  207. <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.5.3.zip">configobj.zip</a> from Voidspace</p>
  208. <blockquote>
  209. <p>This also contains <a class="reference external" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py">validate.py</a>  and <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">this document</a>.</p>
  210. </blockquote>
  211. </li>
  212. <li><p class="first">The latest development version can be obtained from the <a class="reference external" href="http://svn.pythonutils.python-hosting.com/trunk/pythonutils/">Subversion
  213. Repository</a>.</p>
  214. </li>
  215. <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py">validate.py</a> from Voidspace</p>
  216. </li>
  217. <li><p class="first">You can also download <em>configobj.zip</em> from <a class="reference external" href="http://sourceforge.net/projects/configobj">Sourceforge</a></p>
  218. </li>
  219. </ul>
  220. </div>
  221. <div class="section" id="documentation">
  222. <h2><a class="toc-backref" href="#id31">2.2   Documentation</a></h2>
  223. <p><em>configobj.zip</em> also contains <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">this document</a>.</p>
  224. <ul class="simple">
  225. <li>You can view <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">this document</a> online at the <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj Homepage</a>.</li>
  226. </ul>
  227. </div>
  228. <div class="section" id="pythonutils">
  229. <h2><a class="toc-backref" href="#id32">2.3   Pythonutils</a></h2>
  230. <p>ConfigObj is also part of the <a class="reference external" href="http://www.voidspace.org.uk/python/pythonutils.html">Pythonutils</a> set of modules. This contains
  231. various other useful modules, and is required by many of the <a class="reference external" href="http://www.voidspace.org.uk/python/index.shtml">Voidspace Python
  232. Projects</a>.</p>
  233. </div>
  234. <div class="section" id="development-version">
  235. <h2><a class="toc-backref" href="#id33">2.4   Development Version</a></h2>
  236. <p>It is sometimes possible to get the latest <em>development version</em> of ConfigObj
  237. from the <a class="reference external" href="http://svn.pythonutils.python-hosting.com/trunk/pythonutils/">Subversion Repository</a>.</p>
  238. </div>
  239. </div>
  240. <div class="section" id="configobj-in-the-real-world">
  241. <h1><a class="toc-backref" href="#id34">3   ConfigObj in the Real World</a></h1>
  242. <p><strong>ConfigObj</strong> is widely used. Projects using it include:</p>
  243. <ul>
  244. <li><p class="first"><a class="reference external" href="http://bazaar-ng.org">Bazaar</a>.</p>
  245. <blockquote>
  246. <p>Bazaar is a Python distributed <acronym title="Version Control System">VCS</acronym>.
  247. ConfigObj is used to read <tt class="docutils literal"><span class="pre">bazaar.conf</span></tt> and <tt class="docutils literal"><span class="pre">branches.conf</span></tt>.</p>
  248. </blockquote>
  249. </li>
  250. <li><p class="first"><a class="reference external" href="http://www.turbogears.org/">Turbogears</a></p>
  251. <blockquote>
  252. <p>Turbogears is a web application framework.</p>
  253. </blockquote>
  254. </li>
  255. <li><p class="first"><a class="reference external" href="http://chandler.osafoundation.org/">Chandler</a></p>
  256. <blockquote>
  257. <p>A Python and <a class="reference external" href="http://www.wxpython.org">wxPython</a>
  258. <acronym title="Personal Information Manager">PIM</acronym>, being developed by the
  259. <a class="reference external" href="http://www.osafoundation.org/">OSAFoundation</a>.</p>
  260. </blockquote>
  261. </li>
  262. <li><p class="first"><a class="reference external" href="http://matplotlib.sourceforge.net/">matplotlib</a></p>
  263. <blockquote>
  264. <p>A 2D plotting library.</p>
  265. </blockquote>
  266. </li>
  267. <li><p class="first"><a class="reference external" href="http://ipython.scipy.org/moin/">IPython</a></p>
  268. <blockquote>
  269. <p>IPython is an enhanced interactive Python shell. IPython uses ConfigObj in a module called 'TConfig' that combines it with enthought <a class="reference external" href="http://code.enthought.com/traits/">Traits</a>: <a class="reference external" href="http://ipython.scipy.org/ipython/ipython/browser/ipython/branches/saw/sandbox/tconfig">tconfig</a>.</p>
  270. </blockquote>
  271. </li>
  272. <li><p class="first"><a class="reference external" href="http://elisa.fluendo.com/">Elisa - the Fluendo Mediacenter</a></p>
  273. <blockquote>
  274. <p>Elisa is an open source cross-platform media center solution designed to be simple for people not particularly familiar with computers.</p>
  275. </blockquote>
  276. </li>
  277. </ul>
  278. </div>
  279. <div class="section" id="getting-started">
  280. <h1><a class="toc-backref" href="#id35">4   Getting Started</a></h1>
  281. <p>The outstanding feature of using ConfigObj is simplicity. Most functions can be
  282. performed with single line commands.</p>
  283. <div class="section" id="reading-a-config-file">
  284. <h2><a class="toc-backref" href="#id36">4.1   Reading a Config File</a></h2>
  285. <p>The normal way to read a config file, is to give ConfigObj the filename :</p>
  286. <div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
  287. <span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>You can also pass the config file in as a list of lines, or a <tt class="docutils literal"><span class="pre">StringIO</span></tt>
  288. instance, so it doesn't matter where your config data comes from.</p>
  289. <p>You can then access members of your config file as a dictionary. Subsections
  290. will also be dictionaries.</p>
  291. <div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
  292. <span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">)</span><br />
  293. <span class="pycomment">#<br />
  294. </span><span class="pytext">value1</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword1'</span><span class="pyoperator">]</span><br />
  295. <span class="pytext">value2</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword2'</span><span class="pyoperator">]</span><br />
  296. <span class="pycomment">#<br />
  297. </span><span class="pytext">section1</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><br />
  298. <span class="pytext">value3</span> <span class="pyoperator">=</span> <span class="pytext">section1</span><span class="pyoperator">[</span><span class="pystring">'keyword3'</span><span class="pyoperator">]</span><br />
  299. <span class="pytext">value4</span> <span class="pyoperator">=</span> <span class="pytext">section1</span><span class="pyoperator">[</span><span class="pystring">'keyword4'</span><span class="pyoperator">]</span><br />
  300. <span class="pycomment">#<br />
  301. </span><span class="pycomment"># you could also write<br />
  302. </span><span class="pytext">value3</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword3'</span><span class="pyoperator">]</span><br />
  303. <span class="pytext">value4</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword4'</span><span class="pyoperator">]</span><span class="pytext"></span></div></div>
  304. <div class="section" id="writing-a-config-file">
  305. <h2><a class="toc-backref" href="#id37">4.2   Writing a Config File</a></h2>
  306. <p>Creating a new config file is just as easy as reading one. You can specify a
  307. filename when you create the ConfigObj, or do it later <a class="footnote-reference" href="#id17" id="id3">[2]</a>.</p>
  308. <p>If you <em>don't</em> set a filename, then the <tt class="docutils literal"><span class="pre">write</span></tt> method will return a list of
  309. lines instead of writing to file. See the <a class="reference internal" href="#write">write</a> method for more details.</p>
  310. <p>Here we show creating an empty ConfigObj, setting a filename and some values,
  311. and then writing to file :</p>
  312. <div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
  313. <span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  314. <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">filename</span> <span class="pyoperator">=</span> <span class="pytext">filename</span><br />
  315. <span class="pycomment">#<br />
  316. </span><span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword1'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value1</span><br />
  317. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword2'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value2</span><br />
  318. <span class="pycomment">#<br />
  319. </span><span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><span class="pyoperator">}</span><br />
  320. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword3'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value3</span><br />
  321. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword4'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value4</span><br />
  322. <span class="pycomment">#<br />
  323. </span><span class="pytext">section2</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><br />
  324.     <span class="pystring">'keyword5'</span><span class="pyoperator">:</span> <span class="pytext">value5</span><span class="pyoperator">,</span><br />
  325.     <span class="pystring">'keyword6'</span><span class="pyoperator">:</span> <span class="pytext">value6</span><span class="pyoperator">,</span><br />
  326.     <span class="pystring">'sub-section'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
  327.         <span class="pystring">'keyword7'</span><span class="pyoperator">:</span> <span class="pytext">value7</span><br />
  328.         <span class="pyoperator">}</span><br />
  329. <span class="pyoperator">}</span><br />
  330. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section2'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">section2</span><br />
  331. <span class="pycomment">#<br />
  332. </span><span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section3'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><span class="pyoperator">}</span><br />
  333. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section3'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword 8'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">[</span><span class="pytext">value8</span><span class="pyoperator">,</span> <span class="pytext">value9</span><span class="pyoperator">,</span> <span class="pytext">value10</span><span class="pyoperator">]</span><br />
  334. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section3'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword 9'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">[</span><span class="pytext">value11</span><span class="pyoperator">,</span> <span class="pytext">value12</span><span class="pyoperator">,</span> <span class="pytext">value13</span><span class="pyoperator">]</span><br />
  335. <span class="pycomment">#<br />
  336. </span><span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div><div class="caution">
  337. <p class="first admonition-title">Caution!</p>
  338. <p class="last">Keywords and section names can only be strings <a class="footnote-reference" href="#id18" id="id4">[3]</a>. Attempting to set
  339. anything else will raise a <tt class="docutils literal"><span class="pre">ValueError</span></tt>.</p>
  340. </div>
  341. </div>
  342. <div class="section" id="config-files">
  343. <h2><a class="toc-backref" href="#id38">4.3   Config Files</a></h2>
  344. <p>The config files that ConfigObj will read and write are based on the 'INI'
  345. format. This means it will read and write files created for <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>
  346. <a class="footnote-reference" href="#id19" id="id5">[4]</a>.</p>
  347. <p>Keywords and values are separated by an <tt class="docutils literal"><span class="pre">'='</span></tt>, and section markers are
  348. between square brackets. Keywords, values, and section names can be surrounded
  349. by single or double quotes. Indentation is not significant, but can be
  350. preserved.</p>
  351. <p>Subsections are indicated by repeating the square brackets in the section
  352. marker. You nest levels by using more brackets.</p>
  353. <p>You can have list values by separating items with a comma, and values spanning
  354. multiple lines by using triple quotes (single or double).</p>
  355. <p>For full details on all these see <a class="reference internal" href="#the-config-file-format">the config file format</a>. Here's an example
  356. to illustrate :</p>
  357. <pre class="literal-block">
  358. # This is the 'initial_comment'
  359. # Which may be several lines
  360. keyword1 = value1
  361. 'keyword 2' = 'value 2'
  362.  
  363. [ "section 1" ]
  364. # This comment goes with keyword 3
  365. keyword 3 = value 3
  366. 'keyword 4' = value4, value 5, 'value 6'
  367.  
  368.     [[ sub-section ]]    # an inline comment
  369.     # sub-section is inside "section 1"
  370.     'keyword 5' = 'value 7'
  371.     'keyword 6' = '''A multiline value,
  372. that spans more than one line :-)
  373. The line breaks are included in the value.'''
  374.  
  375.         [[[ sub-sub-section ]]]
  376.         # sub-sub-section is *in* 'sub-section'
  377.         # which is in 'section 1'
  378.         'keyword 7' = 'value 8'
  379.  
  380. [section 2]    # an inline comment
  381. keyword8 = "value 9"
  382. keyword9 = value10     # an inline comment
  383. # The 'final_comment'
  384. # Which also may be several lines
  385. </pre>
  386. </div>
  387. </div>
  388. <div class="section" id="configobj-specifications">
  389. <h1><a class="toc-backref" href="#id39">5   ConfigObj specifications</a></h1>
  390. <div class="pysrc"><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">infile</span><span class="pyoperator">=</span><span class="pytext">None</span><span class="pyoperator">,</span> <span class="pytext">options</span><span class="pyoperator">=</span><span class="pytext">None</span><span class="pyoperator">,</span> <span class="pyoperator">**</span><span class="pytext">keywargs</span><span class="pyoperator">)</span><span class="pytext"></span></div><div class="section" id="infile">
  391. <h2><a class="toc-backref" href="#id40">5.1   infile</a></h2>
  392. <p>You don't need to specify an infile. If you omit it, an empty ConfigObj will be
  393. created. <tt class="docutils literal"><span class="pre">infile</span></tt> <em>can</em> be :</p>
  394. <ul class="simple">
  395. <li>Nothing. In which case the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be
  396. <tt class="docutils literal"><span class="pre">None</span></tt>. You can set a filename at any time.</li>
  397. <li>A filename. What happens if the file doesn't already exist is determined by
  398. the <a class="reference internal" href="#options">options</a> <tt class="docutils literal"><span class="pre">file_error</span></tt> and <tt class="docutils literal"><span class="pre">create_empty</span></tt>. The filename will be
  399. preserved as the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute. This can be changed at any time.</li>
  400. <li>A list of lines. Any trailing newlines will be removed from the lines. The
  401. <tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be <tt class="docutils literal"><span class="pre">None</span></tt>.</li>
  402. <li>A <tt class="docutils literal"><span class="pre">StringIO</span></tt> instance or file object, or any object with a <tt class="docutils literal"><span class="pre">read</span></tt> method.
  403. The <tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be <tt class="docutils literal"><span class="pre">None</span></tt> <a class="footnote-reference" href="#id20" id="id6">[5]</a>.</li>
  404. <li>A dictionary. You can initialise a ConfigObj from a dictionary <a class="footnote-reference" href="#id21" id="id7">[6]</a>. The
  405. <tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be <tt class="docutils literal"><span class="pre">None</span></tt>. All keys must be
  406. strings. In this case, the order of values and sections is arbitrary.</li>
  407. </ul>
  408. </div>
  409. <div class="section" id="options">
  410. <h2><a class="toc-backref" href="#id41">5.2   options</a></h2>
  411. <p>There are various options that control the way ConfigObj behaves. They can be
  412. passed in as a dictionary of options, or as keyword arguments. Explicit keyword
  413. arguments override the dictionary.</p>
  414. <p>All of the options are available as attributes after the config file has been
  415. parsed.</p>
  416. <p>ConfigObj has the following options (with the default values shown) :</p>
  417. <ul>
  418. <li><p class="first">'raise_errors': <tt class="docutils literal"><span class="pre">False</span></tt></p>
  419. <blockquote>
  420. <p>When parsing, it is possible that the config file will be badly formed. The
  421. default is to parse the whole file and raise a single error at the end. You
  422. can set <tt class="docutils literal"><span class="pre">raise_errors</span> <span class="pre">=</span> <span class="pre">True</span></tt> to have errors raised immediately. See the
  423. <a class="reference internal" href="#exceptions">exceptions</a> section for more details.</p>
  424. <p>Altering this value after initial parsing has no effect.</p>
  425. </blockquote>
  426. </li>
  427. <li><p class="first">'list_values': <tt class="docutils literal"><span class="pre">True</span></tt></p>
  428. <blockquote>
  429. <p>If <tt class="docutils literal"><span class="pre">True</span></tt> (the default) then list values are possible. If <tt class="docutils literal"><span class="pre">False</span></tt>, the
  430. values are not parsed for lists.</p>
  431. <blockquote>
  432. <p>If <tt class="docutils literal"><span class="pre">list_values</span> <span class="pre">=</span> <span class="pre">False</span></tt> then single line values are not quoted or
  433. unquoted when reading and writing.</p>
  434. </blockquote>
  435. <p>Changing this value affects whether single line values will be quoted or
  436. not when writing.</p>
  437. </blockquote>
  438. </li>
  439. <li><p class="first">'create_empty': <tt class="docutils literal"><span class="pre">False</span></tt></p>
  440. <blockquote>
  441. <p>If this value is <tt class="docutils literal"><span class="pre">True</span></tt> and the file specified by <tt class="docutils literal"><span class="pre">infile</span></tt> doesn't
  442. exist, ConfigObj will create an empty file. This can be a useful test that
  443. the filename makes sense: an impossible filename will cause an error.</p>
  444. <p>Altering this value after initial parsing has no effect.</p>
  445. </blockquote>
  446. </li>
  447. <li><p class="first">'file_error': <tt class="docutils literal"><span class="pre">False</span></tt></p>
  448. <blockquote>
  449. <p>If this value is <tt class="docutils literal"><span class="pre">True</span></tt> and the file specified by <tt class="docutils literal"><span class="pre">infile</span></tt> doesn't
  450. exist, ConfigObj will raise an <tt class="docutils literal"><span class="pre">IOError</span></tt>.</p>
  451. <p>Altering this value after initial parsing has no effect.</p>
  452. </blockquote>
  453. </li>
  454. <li><p class="first">'interpolation': <tt class="docutils literal"><span class="pre">True</span></tt></p>
  455. <blockquote>
  456. <p>Whether string interpolation is switched on or not. It is on (<tt class="docutils literal"><span class="pre">True</span></tt>) by
  457. default.</p>
  458. <p>You can set this attribute to change whether string interpolation is done
  459. when values are fetched. See the <a class="reference internal" href="#string-interpolation">String Interpolation</a> section for more details.</p>
  460. </blockquote>
  461. </li>
  462. <li><p class="first">'configspec': <tt class="docutils literal"><span class="pre">None</span></tt></p>
  463. <blockquote>
  464. <p>If you want to use the validation system, you supply a configspec. This is
  465. effectively a type of config file that specifies a check for each member.
  466. This check can be used to do type conversion as well as check that the
  467. value is within your required parameters.</p>
  468. <p>You provide a configspec in the same way as you do the initial file: a
  469. filename, or list of lines, etc. See the <a class="reference internal" href="#validation">validation</a> section for full
  470. details on how to use the system.</p>
  471. <p>When parsed, every section has a <tt class="docutils literal"><span class="pre">configspec</span></tt> with a dictionary of
  472. configspec checks for <em>that section</em>.</p>
  473. </blockquote>
  474. </li>
  475. <li><p class="first">'stringify': <tt class="docutils literal"><span class="pre">True</span></tt></p>
  476. <blockquote>
  477. <p>If you use the validation scheme, it can do type checking <em>and</em> conversion
  478. for you. This means you may want to set members to integers, or other
  479. non-string values.</p>
  480. <p>If 'stringify' is set to <tt class="docutils literal"><span class="pre">True</span></tt> (default) then non-string values will
  481. be converted to strings when you write the config file. The <a class="reference internal" href="#validation">validation</a>
  482. process converts values from strings to the required type.</p>
  483. <p>If 'stringify' is set to <tt class="docutils literal"><span class="pre">False</span></tt>, attempting to set a member to a
  484. non-string value <a class="footnote-reference" href="#id22" id="id8">[7]</a> will raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt> (no type conversion is
  485. done by validation).</p>
  486. </blockquote>
  487. </li>
  488. <li><p class="first">'indent_type': <tt class="docutils literal"><span class="pre">'</span>    <span class="pre">'</span></tt></p>
  489. <blockquote>
  490. <p>Indentation is not significant; it can however be present in the input and
  491. output config. Any combination of tabs and spaces may be used: the string
  492. will be repeated for each level of indentation. Typical values are: <tt class="docutils literal"><span class="pre">''</span></tt>
  493. (no indentation), <tt class="docutils literal"><span class="pre">'</span>    <span class="pre">'</span></tt> (indentation with four spaces, the default),
  494. <tt class="docutils literal"><span class="pre">'\t'</span></tt> (indentation with one tab).</p>
  495. <p>If this option is not specified, and the ConfigObj is initialised with a
  496. dictionary, the indentation used in the output is the default one, that is,
  497. four spaces.</p>
  498. <p>If this option is not specified, and the ConfigObj is initialised with a
  499. list of lines or a file, the indentation used in the first indented line is
  500. selected and used in all output lines. If no input line is indented, no
  501. output line will be either.</p>
  502. <p>If this option <em>is</em> specified, the option value is used in the output
  503. config, overriding the type of indentation in the input config (if any).</p>
  504. </blockquote>
  505. </li>
  506. <li><p class="first">'encoding': <tt class="docutils literal"><span class="pre">None</span></tt></p>
  507. <blockquote>
  508. <p>By default <strong>ConfigObj</strong> does not decode the file/strings you pass it into
  509. Unicode <a class="footnote-reference" href="#id23" id="id9">[8]</a>. If you want your config file as Unicode (keys and members)
  510. you need to provide an encoding to decode the file with. This encoding will
  511. also be used to encode the config file when writing.</p>
  512. <p>You can change the encoding attribute at any time.</p>
  513. <p>Any characters in your strings that can't be encoded with the specified
  514. encoding will raise a <tt class="docutils literal"><span class="pre">UnicodeEncodeError</span></tt>.</p>
  515. <div class="note">
  516. <p class="first admonition-title">Note</p>
  517. <p><tt class="docutils literal"><span class="pre">UTF16</span></tt> encoded files will automatically be detected and decoded,
  518. even if <tt class="docutils literal"><span class="pre">encoding</span></tt> is <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
  519. <p class="last">This is because it is a 16-bit encoding, and ConfigObj will mangle it
  520. (split characters on byte boundaries) if it parses it without decoding.</p>
  521. </div>
  522. </blockquote>
  523. </li>
  524. <li><p class="first">'default_encoding': <tt class="docutils literal"><span class="pre">None</span></tt></p>
  525. <blockquote>
  526. <p>When using the <tt class="docutils literal"><span class="pre">write</span></tt> method, <strong>ConfigObj</strong> uses the <tt class="docutils literal"><span class="pre">encoding</span></tt>
  527. attribute to encode the Unicode strings. If any members (or keys) have
  528. been set as byte strings instead of Unicode, these must first be decoded
  529. to Unicode before outputting in the specified encoding.</p>
  530. <p><tt class="docutils literal"><span class="pre">default_encoding</span></tt>, if specified, is the encoding used to decode byte
  531. strings in the <strong>ConfigObj</strong> before writing. If this is <tt class="docutils literal"><span class="pre">None</span></tt>, then
  532. the Python default encoding (<tt class="docutils literal"><span class="pre">sys.defaultencoding</span></tt> - usually ASCII) is
  533. used.</p>
  534. <p>For most Western European users, a value of <tt class="docutils literal"><span class="pre">latin-1</span></tt> is sensible.</p>
  535. <p><tt class="docutils literal"><span class="pre">default_encoding</span></tt> is <em>only</em> used if an <tt class="docutils literal"><span class="pre">encoding</span></tt> is specified.</p>
  536. <p>Any characters in byte-strings that can't be decoded using the
  537. <tt class="docutils literal"><span class="pre">default_encoding</span></tt> will raise a <tt class="docutils literal"><span class="pre">UnicodeDecodeError</span></tt>.</p>
  538. </blockquote>
  539. </li>
  540. <li><p class="first">'unrepr': <tt class="docutils literal"><span class="pre">False</span></tt></p>
  541. <blockquote>
  542. <p>The <tt class="docutils literal"><span class="pre">unrepr</span></tt> option reads and writes files in a different mode. This
  543. allows you to store and retrieve the basic Python data-types using config
  544. files.</p>
  545. <p>This uses Python syntax for lists and quoting. See <a class="reference internal" href="#unrepr-mode">unrepr mode</a> for the
  546. full details.</p>
  547. </blockquote>
  548. </li>
  549. <li><p class="first">'write_empty_values': <tt class="docutils literal"><span class="pre">False</span></tt></p>
  550. <blockquote>
  551. <p>If <tt class="docutils literal"><span class="pre">write_empty_values</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, empty strings are written as
  552. empty values. See <a class="reference internal" href="#empty-values">Empty Values</a> for more details.</p>
  553. </blockquote>
  554. </li>
  555. </ul>
  556. </div>
  557. <div class="section" id="methods">
  558. <h2><a class="toc-backref" href="#id42">5.3   Methods</a></h2>
  559. <p>The ConfigObj is a subclass of an object called <tt class="docutils literal"><span class="pre">Section</span></tt>, which is itself a
  560. subclass of <tt class="docutils literal"><span class="pre">dict</span></tt>, the builtin dictionary type. This means it also has
  561. <strong>all</strong> the normal dictionary methods.</p>
  562. <p>In addition, the following <a class="reference internal" href="#section-methods">Section Methods</a> may be useful :</p>
  563. <ul class="simple">
  564. <li>'restore_default'</li>
  565. <li>'restore_defaults'</li>
  566. <li>'walk'</li>
  567. <li>'merge'</li>
  568. <li>'dict'</li>
  569. <li>'as_bool'</li>
  570. <li>'as_float'</li>
  571. <li>'as_int'</li>
  572. </ul>
  573. <p>Read about <a class="reference internal" href="#sections">Sections</a> for details of all the methods.</p>
  574. <div class="hint">
  575. <p class="first admonition-title">Hint</p>
  576. <p>The <em>merge</em> method of sections is a recursive update.</p>
  577. <p>You can use this to merge sections, or even whole ConfigObjs, into each
  578. other.</p>
  579. <p class="last">You would typically use this to create a default ConfigObj and then merge
  580. in user settings. This way users only need to specify values that are
  581. different from the default. You can use configspecs and validation to
  582. achieve the same thing of course.</p>
  583. </div>
  584. <p>The public methods available on ConfigObj are :</p>
  585. <ul class="simple">
  586. <li>'write'</li>
  587. <li>'validate'</li>
  588. <li>'reset'</li>
  589. <li>'reload'</li>
  590. </ul>
  591. <div class="section" id="write">
  592. <h3><a class="toc-backref" href="#id43">5.3.1   write</a></h3>
  593. <pre class="literal-block">
  594. write(file_object=None)
  595. </pre>
  596. <p>This method writes the current ConfigObj and takes a single, optional argument
  597. <a class="footnote-reference" href="#id24" id="id10">[9]</a>.</p>
  598. <p>If you pass in a file like object to the <tt class="docutils literal"><span class="pre">write</span></tt> method, the config file will
  599. be written to this. (The only method of this object that is used is its
  600. <tt class="docutils literal"><span class="pre">write</span></tt> method, so a <tt class="docutils literal"><span class="pre">StringIO</span></tt> instance, or any other file like object
  601. will work.)</p>
  602. <p>Otherwise, the behaviour of this method depends on the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute
  603. of the ConfigObj.</p>
  604. <dl class="docutils">
  605. <dt><tt class="docutils literal"><span class="pre">filename</span></tt></dt>
  606. <dd>ConfigObj will write the configuration to the file specified.</dd>
  607. <dt><tt class="docutils literal"><span class="pre">None</span></tt></dt>
  608. <dd><tt class="docutils literal"><span class="pre">write</span></tt> returns a list of lines. (Not <tt class="docutils literal"><span class="pre">'\n'</span></tt> terminated)</dd>
  609. </dl>
  610. <p>First the 'initial_comment' is written, then the config file, followed by the
  611. 'final_comment'. Comment lines and inline comments are written with each
  612. key/value.</p>
  613. </div>
  614. <div class="section" id="validate">
  615. <h3><a class="toc-backref" href="#id44">5.3.2   validate</a></h3>
  616. <pre class="literal-block">
  617. validate(validator, preserve_errors=False, copy=False)
  618. </pre>
  619. <div class="pysrc"><span class="pycomment"># filename is the config file<br />
  620. </span><span class="pycomment"># filename2 is the configspec<br />
  621. </span><span class="pycomment"># (which could also be hardcoded into your program)<br />
  622. </span><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">filename2</span><span class="pyoperator">)</span><br />
  623. <span class="pycomment">#<br />
  624. </span><span class="pykeyword">from</span> <span class="pytext">validate</span> <span class="pykeyword">import</span> <span class="pytext">Validator</span><br />
  625. <span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">Validator</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  626. <span class="pytext">test</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">)</span><br />
  627. <span class="pykeyword">if</span> <span class="pytext">test</span> <span class="pyoperator">==</span> <span class="pytext">True</span><span class="pyoperator">:</span><br />
  628.     <span class="pykeyword">print</span> <span class="pystring">'Succeeded.'</span><span class="pytext"></span></div><p>The validate method uses the <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">validate</a> module to do the
  629. validation.</p>
  630. <p>This method validates the ConfigObj against the configspec. By doing type
  631. conversion as well it can abstract away the config file altogether and present
  632. the config <em>data</em> to your application (in the types it expects it to be).</p>
  633. <p>If the <tt class="docutils literal"><span class="pre">configspec</span></tt> attribute of the ConfigObj is <tt class="docutils literal"><span class="pre">None</span></tt>, it raises a
  634. <tt class="docutils literal"><span class="pre">ValueError</span></tt>.</p>
  635. <p>If the <a class="reference internal" href="#stringify">stringify</a> attribute is set, this process will convert values to the
  636. type defined in the configspec.</p>
  637. <p>The validate method uses checks specified in the configspec and defined in the
  638. <tt class="docutils literal"><span class="pre">Validator</span></tt> object. It is very easy to extend.</p>
  639. <p>The configspec looks like the config file, but instead of the value, you
  640. specify the check (and any default value). See the <a class="reference internal" href="#validation">validation</a> section for
  641. details.</p>
  642. <div class="hint">
  643. <p class="first admonition-title">Hint</p>
  644. <p class="last">The system of configspecs can seem confusing at first, but is actually
  645. quite simple and powerful. For a concrete example of how to use it, you may
  646. find this blog entry helpful :
  647. <a class="reference external" href="http://www.voidspace.org.uk/python/weblog/arch_d7_2006_03_04.shtml#e257">Transforming Values with ConfigObj</a>.</p>
  648. </div>
  649. <p>The <tt class="docutils literal"><span class="pre">copy</span></tt> parameter fills in missing values from the configspec (default
  650. values), <em>without</em> marking the values as defaults. It also causes comments to
  651. be copied from the configspec into the config file. This allows you to use a
  652. configspec to create default config files. (Normally default values aren't
  653. written out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.)</p>
  654. <p>As of ConfigObj 4.3.0 you can also pass in a ConfigObj instance as your
  655. configspec. This is especially useful if you need to specify the encoding of
  656. your configspec file. When you read your configspec file, you <em>must</em> specify
  657. <tt class="docutils literal"><span class="pre">list_values=False</span></tt>.</p>
  658. <div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
  659. <span class="pytext">configspec</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">configspecfilename</span><span class="pyoperator">,</span> <span class="pytext">encoding</span><span class="pyoperator">=</span><span class="pystring">'UTF8'</span><span class="pyoperator">,</span><br />
  660.                        <span class="pytext">list_values</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">)</span><br />
  661. <span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">configspec</span><span class="pyoperator">)</span><span class="pytext"></span></div><div class="section" id="return-value">
  662. <h4><a class="toc-backref" href="#id45">5.3.2.1   Return Value</a></h4>
  663. <p>By default, the validate method either returns <tt class="docutils literal"><span class="pre">True</span></tt> (everything passed)
  664. or a dictionary of <tt class="docutils literal"><span class="pre">True</span></tt>/<tt class="docutils literal"><span class="pre">False</span></tt> representing pass/fail. The dictionary
  665. follows the structure of the ConfigObj.</p>
  666. <p>If a whole section passes then it is replaced with the value <tt class="docutils literal"><span class="pre">True</span></tt>. If a
  667. whole section fails, then it is replaced with the value <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
  668. <p>If a value is missing, and there is no default in the check, then the check
  669. automatically fails.</p>
  670. <p>The <tt class="docutils literal"><span class="pre">validate</span></tt> method takes an optional keyword argument <tt class="docutils literal"><span class="pre">preserve_errors</span></tt>.
  671. If you set this to <tt class="docutils literal"><span class="pre">True</span></tt>, instead of getting <tt class="docutils literal"><span class="pre">False</span></tt> for failed checks you
  672. get the actual error object from the <strong>validate</strong> module. This usually contains
  673. useful information about why the check failed.</p>
  674. <p>See the <a class="reference internal" href="#flatten-errors">flatten_errors</a> function for how to turn your results dictionary into
  675. a useful list of error messages.</p>
  676. <p>Even if <tt class="docutils literal"><span class="pre">preserve_errors</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, missing keys or sections will still be
  677. represented by a <tt class="docutils literal"><span class="pre">False</span></tt> in the results dictionary.</p>
  678. </div>
  679. <div class="section" id="mentioning-default-values">
  680. <h4><a class="toc-backref" href="#id46">5.3.2.2   Mentioning Default Values</a></h4>
  681. <p>In the check in your configspec, you can specify a default to be used - by
  682. using the <tt class="docutils literal"><span class="pre">default</span></tt> keyword. E.g.</p>
  683. <pre class="literal-block">
  684. key1 = integer(0, 30, default=15)
  685. key2 = integer(default=15)
  686. key3 = boolean(default=True)
  687. key4 = option('Hello', 'Goodbye', 'Not Today', default='Not Today')
  688. </pre>
  689. <p>If the configspec check supplies a default and the value is missing in the
  690. config, then the default will be set in your ConfigObj. (It is still passed to
  691. the <tt class="docutils literal"><span class="pre">Validator</span></tt> so that type conversion can be done: this means the default
  692. value must still pass the check.)</p>
  693. <p>ConfigObj keeps a record of which values come from defaults, using the
  694. <tt class="docutils literal"><span class="pre">defaults</span></tt> attribute of <a class="reference internal" href="#sections">sections</a>. Any key in this list isn't written out by
  695. the <tt class="docutils literal"><span class="pre">write</span></tt> method. If a key is set from outside (even to the same value)
  696. then it is removed from the <tt class="docutils literal"><span class="pre">defaults</span></tt> list.</p>
  697. <!-- note:
  698.  
  699. Even if all the keys in a section are in the defaults list, the section
  700. marker is still written out. -->
  701. <p>There is additionally a special case default value of <tt class="docutils literal"><span class="pre">None</span></tt>. If you set the
  702. default value to <tt class="docutils literal"><span class="pre">None</span></tt> and the value is missing, the value will always be
  703. set to <tt class="docutils literal"><span class="pre">None</span></tt>. As the other checks don't return <tt class="docutils literal"><span class="pre">None</span></tt> (unless you
  704. implement your own that do), you can tell that this value came from a default
  705. value (and was missing from the config file). It allows an easy way of
  706. implementing optional values. Simply check (and ignore) members that are set
  707. to <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
  708. <div class="note">
  709. <p class="first admonition-title">Note</p>
  710. <p class="last">If <a class="reference internal" href="#stringify">stringify</a> is <tt class="docutils literal"><span class="pre">False</span></tt> then <tt class="docutils literal"><span class="pre">default=None</span></tt> returns <tt class="docutils literal"><span class="pre">''</span></tt> instead of
  711. <tt class="docutils literal"><span class="pre">None</span></tt>. This is because setting a value to a non-string raises an error
  712. if stringify is unset.</p>
  713. </div>
  714. <p>The default value can be a list. See <a class="reference internal" href="#id15">List Values</a> for the way to do this.</p>
  715. <p>Writing invalid default values is a <em>guaranteed</em> way of confusing your users.
  716. Default values <strong>must</strong> pass the check.</p>
  717. </div>
  718. <div class="section" id="mentioning-repeated-sections">
  719. <h4><a class="toc-backref" href="#id47">5.3.2.3   Mentioning Repeated Sections</a></h4>
  720. <p>In the configspec it is possible to cause <em>every</em> sub-section in a section to
  721. be validated using the same configspec. You do this with a section in the
  722. configspec  called <tt class="docutils literal"><span class="pre">__many__</span></tt>. Every sub-section in that section has the
  723. <tt class="docutils literal"><span class="pre">__many__</span></tt> configspec applied to it (without you having to explicitly name
  724. them in advance).</p>
  725. <p>If you define a <tt class="docutils literal"><span class="pre">__many__</span></tt> type section it must the only sub-section in that
  726. section. Having a <tt class="docutils literal"><span class="pre">__many__</span></tt> <em>and</em> other sub-sections defined in the same
  727. section will raise a <tt class="docutils literal"><span class="pre">RepeatSectionError</span></tt>.</p>
  728. <p>Your <tt class="docutils literal"><span class="pre">__many__</span></tt> section can have nested subsections, which can also include
  729. <tt class="docutils literal"><span class="pre">__many__</span></tt> type sections.</p>
  730. <p>See <a class="reference internal" href="#repeated-sections">Repeated Sections</a> for examples.</p>
  731. </div>
  732. <div class="section" id="mentioning-simpleval">
  733. <h4><a class="toc-backref" href="#id48">5.3.2.4   Mentioning SimpleVal</a></h4>
  734. <p>If you just want to check if all members are present, then you can use the
  735. <tt class="docutils literal"><span class="pre">SimpleVal</span></tt> object that comes with ConfigObj. It only fails members if they
  736. are missing.</p>
  737. <p>Write a configspec that has all the members you want to check for, but set
  738. every section to <tt class="docutils literal"><span class="pre">''</span></tt>.</p>
  739. <div class="pysrc"><span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">SimpleVal</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  740. <span class="pytext">test</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">)</span><br />
  741. <span class="pykeyword">if</span> <span class="pytext">test</span> <span class="pykeyword">is</span> <span class="pytext">True</span><span class="pyoperator">:</span><br />
  742.     <span class="pykeyword">print</span> <span class="pystring">'Succeeded.'</span><span class="pytext"></span></div></div>
  743. <div class="section" id="mentioning-copy-mode">
  744. <h4><a class="toc-backref" href="#id49">5.3.2.5   Mentioning copy Mode</a></h4>
  745. <p>As discussed in <a class="reference internal" href="#mentioning-default-values">Mentioning Default Values</a>, you can use a configspec to
  746. supply default values. These are marked in the ConfigObj instance as defaults,
  747. and <em>not</em> written out by the <tt class="docutils literal"><span class="pre">write</span></tt> mode. This means that your users only
  748. need to supply values that are different from the defaults.</p>
  749. <p>This can be inconvenient if you <em>do</em> want to write out the default values,
  750. for example to write out a default config file.</p>
  751. <p>If you set <tt class="docutils literal"><span class="pre">copy=True</span></tt> when you call validate, then no values are marked as
  752. defaults. In addition, all comments from the configspec are copied into
  753. your ConfigObj instance. You can then call <tt class="docutils literal"><span class="pre">write</span></tt> to create your config
  754. file.</p>
  755. <p>There is a limitation with this. In order to allow <a class="reference internal" href="#string-interpolation">String Interpolation</a> to work
  756. within configspecs, <tt class="docutils literal"><span class="pre">DEFAULT</span></tt> sections are not processed by
  757. validation; even in copy mode.</p>
  758. </div>
  759. </div>
  760. <div class="section" id="reload">
  761. <h3><a class="toc-backref" href="#id50">5.3.3   reload</a></h3>
  762. <p>If a ConfigObj instance was loaded from the filesystem, then this method will reload it. It
  763. will also reuse any configspec you supplied at instantiation (including reloading it from
  764. the filesystem if you passed it in as a filename).</p>
  765. <p>If the ConfigObj does not have a filename attribute pointing to a file, then a <tt class="docutils literal"><span class="pre">ReloadError</span></tt>
  766. will be raised.</p>
  767. </div>
  768. <div class="section" id="reset">
  769. <h3><a class="toc-backref" href="#id51">5.3.4   reset</a></h3>
  770. <p>This method takes no arguments and doesn't return anything. It restores a ConfigObj
  771. instance to a freshly created state.</p>
  772. </div>
  773. </div>
  774. <div class="section" id="attributes">
  775. <h2><a class="toc-backref" href="#id52">5.4   Attributes</a></h2>
  776. <p>A ConfigObj has the following attributes :</p>
  777. <ul class="simple">
  778. <li>indent_type</li>
  779. <li>interpolate</li>
  780. <li>stringify</li>
  781. <li>BOM</li>
  782. <li>initial_comment</li>
  783. <li>final_comment</li>
  784. <li>list_values</li>
  785. <li>encoding</li>
  786. <li>default_encoding</li>
  787. <li>unrepr</li>
  788. <li>write_empty_values</li>
  789. <li>newlines</li>
  790. </ul>
  791. <div class="note">
  792. <p class="first admonition-title">Note</p>
  793. <p class="last">This doesn't include <em>comments</em>, <em>inline_comments</em>, <em>defaults</em>, or
  794. <em>configspec</em>. These are actually attributes of <a class="reference internal" href="#sections">Sections</a>.</p>
  795. </div>
  796. <p>It also has the following attributes as a result of parsing. They correspond to
  797. <a class="reference internal" href="#options">options</a> when the ConfigObj was created, but changing them has no effect.</p>
  798. <ul class="simple">
  799. <li>raise_errors</li>
  800. <li>create_empty</li>
  801. <li>file_error</li>
  802. </ul>
  803. <div class="section" id="interpolation">
  804. <h3><a class="toc-backref" href="#id53">5.4.1   interpolation</a></h3>
  805. <p>ConfigObj can perform string interpolation in a <em>similar</em> way to
  806. <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>. See the <a class="reference internal" href="#string-interpolation">String Interpolation</a> section for full details.</p>
  807. <p>If <tt class="docutils literal"><span class="pre">interpolation</span></tt> is set to <tt class="docutils literal"><span class="pre">False</span></tt>, then interpolation is <em>not</em> done when
  808. you fetch values.</p>
  809. </div>
  810. <div class="section" id="stringify">
  811. <h3><a class="toc-backref" href="#id54">5.4.2   stringify</a></h3>
  812. <p>If this attribute is set (<tt class="docutils literal"><span class="pre">True</span></tt>) then the <a class="reference internal" href="#validate">validate</a> method changes the
  813. values in the ConfigObj. These are turned back into strings when <a class="reference internal" href="#write">write</a> is
  814. called.</p>
  815. <p>If stringify is unset (<tt class="docutils literal"><span class="pre">False</span></tt>) then attempting to set a value to a non
  816. string (or a list of strings) will raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt>.</p>
  817. </div>
  818. <div class="section" id="bom">
  819. <h3><a class="toc-backref" href="#id55">5.4.3   BOM</a></h3>
  820. <p>If the initial config file <em>started</em> with the UTF8 Unicode signature (known
  821. slightly incorrectly as the <acronym title="Byte Order Mark">BOM</acronym>), or the UTF16 BOM, then
  822. this attribute is set to <tt class="docutils literal"><span class="pre">True</span></tt>. Otherwise it is <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
  823. <p>If it is set to <tt class="docutils literal"><span class="pre">True</span></tt> when <tt class="docutils literal"><span class="pre">write</span></tt> is called then, if <tt class="docutils literal"><span class="pre">encoding</span></tt> is set
  824. to <tt class="docutils literal"><span class="pre">None</span></tt> <em>or</em> to <tt class="docutils literal"><span class="pre">utf_8</span></tt> (and variants) a UTF BOM will be written.</p>
  825. <p>For UTF16 encodings, a BOM is <em>always</em> written.</p>
  826. </div>
  827. <div class="section" id="initial-comment">
  828. <h3><a class="toc-backref" href="#id56">5.4.4   initial_comment</a></h3>
  829. <p>This is a list of lines. If the ConfigObj is created from an existing file, it
  830. will contain any lines of comments before the start of the members.</p>
  831. <p>If you create a new ConfigObj, this will be an empty list.</p>
  832. <p>The write method puts these lines before it starts writing out the members.</p>
  833. </div>
  834. <div class="section" id="final-comment">
  835. <h3><a class="toc-backref" href="#id57">5.4.5   final_comment</a></h3>
  836. <p>This is a list of lines. If the ConfigObj is created from an existing file, it
  837. will contain any lines of comments after the last member.</p>
  838. <p>If you create a new ConfigObj, this will be an empty list.</p>
  839. <p>The <tt class="docutils literal"><span class="pre">write</span></tt> method puts these lines after it finishes writing out the
  840. members.</p>
  841. </div>
  842. <div class="section" id="list-values">
  843. <h3><a class="toc-backref" href="#id58">5.4.6   list_values</a></h3>
  844. <p>This attribute is <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>. If set to <tt class="docutils literal"><span class="pre">False</span></tt> then values are
  845. not parsed for list values. In addition single line values are not unquoted.</p>
  846. <p>This allows you to do your own parsing of values. It exists primarily to
  847. support the reading of the <a class="reference internal" href="#configspec">configspec</a> - but has other use cases.</p>
  848. <p>For example you could use the <tt class="docutils literal"><span class="pre">LineParser</span></tt> from the
  849. <a class="reference external" href="http://www.voidspace.org.uk/python/listquote.html#lineparser">listquote module</a>
  850. to read values for nested lists.</p>
  851. <p>Single line values aren't quoted when writing - but multiline values are
  852. handled as normal.</p>
  853. <div class="caution">
  854. <p class="first admonition-title">Caution!</p>
  855. <dl class="docutils">
  856. <dt>Because values aren't quoted, leading or trailing whitespace can be</dt>
  857. <dd>lost.</dd>
  858. </dl>
  859. <p>This behaviour was changed in version 4.0.1.</p>
  860. <blockquote class="last">
  861. Prior to this, single line values might have been quoted; even with
  862. <tt class="docutils literal"><span class="pre">list_values=False</span></tt>. This means that files written by <strong>ConfigObj</strong>
  863. <em>could</em> now be incompatible - and need the quotes removing by hand.</blockquote>
  864. </div>
  865. </div>
  866. <div class="section" id="encoding">
  867. <h3><a class="toc-backref" href="#id59">5.4.7   encoding</a></h3>
  868. <p>This is the encoding used to encode the output, when you call <tt class="docutils literal"><span class="pre">write</span></tt>. It
  869. must be a valid encoding <a class="reference external" href="http://docs.python.org/lib/standard-encodings.html">recognised by Python</a>.</p>
  870. <p>If this value is <tt class="docutils literal"><span class="pre">None</span></tt> then no encoding is done when <tt class="docutils literal"><span class="pre">write</span></tt> is called.</p>
  871. </div>
  872. <div class="section" id="default-encoding">
  873. <h3><a class="toc-backref" href="#id60">5.4.8   default_encoding</a></h3>
  874. <p>If encoding is set, any byte-strings in your ConfigObj instance (keys or
  875. members) will first be decoded to Unicode using the encoding specified by the
  876. <tt class="docutils literal"><span class="pre">default_encoding</span></tt> attribute. This ensures that the output is in the encoding
  877. specified.</p>
  878. <p>If this value is <tt class="docutils literal"><span class="pre">None</span></tt> then <tt class="docutils literal"><span class="pre">sys.defaultencoding</span></tt> is used instead.</p>
  879. </div>
  880. <div class="section" id="unrepr">
  881. <h3><a class="toc-backref" href="#id61">5.4.9   unrepr</a></h3>
  882. <p>Another boolean value. If this is set, then <tt class="docutils literal"><span class="pre">repr(value)</span></tt> is used to write
  883. values. This writes values in a slightly different way to the normal ConfigObj
  884. file syntax.</p>
  885. <p>This preserves basic Python data-types when read back in. See <a class="reference internal" href="#unrepr-mode">unrepr mode</a>
  886. for more details.</p>
  887. </div>
  888. <div class="section" id="write-empty-values">
  889. <h3><a class="toc-backref" href="#id62">5.4.10   write_empty_values</a></h3>
  890. <p>Also boolean. If set, values that are an empty string (<tt class="docutils literal"><span class="pre">''</span></tt>) are written as
  891. empty values. See <a class="reference internal" href="#empty-values">Empty Values</a> for more details.</p>
  892. </div>
  893. <div class="section" id="newlines">
  894. <h3><a class="toc-backref" href="#id63">5.4.11   newlines</a></h3>
  895. <p>When a config file is read, ConfigObj records the type of newline separators in the
  896. file and uses this separator when writing. It defaults to <tt class="docutils literal"><span class="pre">None</span></tt>, and ConfigObj
  897. uses the system default (<tt class="docutils literal"><span class="pre">os.sep</span></tt>) if write is called without newlines having
  898. been set.</p>
  899. </div>
  900. </div>
  901. </div>
  902. <div class="section" id="the-config-file-format">
  903. <h1><a class="toc-backref" href="#id64">6   The Config File Format</a></h1>
  904. <p>You saw an example config file in the <a class="reference internal" href="#config-files">Config Files</a> section. Here is a fuller
  905. specification of the config files used and created by ConfigObj.</p>
  906. <p>The basic pattern for keywords is :</p>
  907. <pre class="literal-block">
  908. # comment line
  909. # comment line
  910. keyword = value # inline comment
  911. </pre>
  912. <p>Both keyword and value can optionally be surrounded in quotes. The equals sign
  913. is the only valid divider.</p>
  914. <p>Values can have comments on the lines above them, and an inline comment after
  915. them. This, of course, is optional. See the <a class="reference internal" href="#comments">comments</a> section for details.</p>
  916. <p>If a keyword or value starts or ends with whitespace, or contains a quote mark
  917. or comma, then it should be surrounded by quotes. Quotes are not necessary if
  918. whitespace is surrounded by non-whitespace.</p>
  919. <p>Values can also be lists. Lists are comma separated. You indicate a single
  920. member list by a trailing comma. An empty list is shown by a single comma :</p>
  921. <pre class="literal-block">
  922. keyword1 = value1, value2, value3
  923. keyword2 = value1, # a single member list
  924. keyword3 = , # an empty list
  925. </pre>
  926. <p>Values that contain line breaks (multi-line values) can be surrounded by triple
  927. quotes. These can also be used if a value contains both types of quotes. List
  928. members cannot be surrounded by triple quotes :</p>
  929. <pre class="literal-block">
  930. keyword1 = ''' A multi line value
  931. on several
  932. lines'''     # with a comment
  933. keyword2 = '''I won't be "afraid".'''
  934. #
  935. keyword3 = """ A multi line value
  936. on several
  937. lines"""     # with a comment
  938. keyword4 = """I won't be "afraid"."""
  939. </pre>
  940. <div class="warning">
  941. <p class="first admonition-title">Warning</p>
  942. <p class="last">There is no way of safely quoting values that contain both types of triple
  943. quotes.</p>
  944. </div>
  945. <p>A line that starts with a '#', possibly preceded by whitespace, is a comment.</p>
  946. <p>New sections are indicated by a section marker line. That is the section name
  947. in square brackets. Whitespace around the section name is ignored. The name can
  948. be quoted with single or double quotes. The marker can have comments before it
  949. and an inline comment after it :</p>
  950. <pre class="literal-block">
  951. # The First Section
  952. [ section name 1 ] # first section
  953. keyword1 = value1
  954.  
  955. # The Second Section
  956. [ "section name 2" ] # second section
  957. keyword2 = value2
  958. </pre>
  959. <p>Any subsections (sections that are <em>inside</em> the current section) are
  960. designated by repeating the square brackets before and after the section name.
  961. The number of square brackets represents the nesting level of the sub-section.
  962. Square brackets may be separated by whitespace; such whitespace, however, will
  963. not be present in the output config written by the <tt class="docutils literal"><span class="pre">write</span></tt> method.</p>
  964. <p>Indentation is not significant, but can be preserved. See the description of
  965. the <tt class="docutils literal"><span class="pre">indent_type</span></tt> option, in the <a class="reference internal" href="#configobj-specifications">ConfigObj specifications</a> chapter, for the
  966. details.</p>
  967. <p>A <em>NestingError</em> will be raised if the number of the opening and the closing
  968. brackets in a section marker is not the same, or if a sub-section's nesting
  969. level is greater than the nesting level of it parent plus one.</p>
  970. <p>In the outer section, single values can only appear before any sub-section.
  971. Otherwise they will belong to the sub-section immediately before them.</p>
  972. <pre class="literal-block">
  973. # initial comment
  974. keyword1 = value1
  975. keyword2 = value2
  976.  
  977. [section 1]
  978. keyword1 = value1
  979. keyword2 = value2
  980.  
  981.     [[sub-section]]
  982.     # this is in section 1
  983.     keyword1 = value1
  984.     keyword2 = value2
  985.  
  986.         [[[nested section]]]
  987.         # this is in sub section
  988.         keyword1 = value1
  989.         keyword2 = value2
  990.  
  991.     [[sub-section2]]
  992.     # this is in section 1 again
  993.     keyword1 = value1
  994.     keyword2 = value2
  995.  
  996. [[sub-section3]]
  997. # this is also in section 1, indentation is misleading here
  998. keyword1 = value1
  999. keyword2 = value2
  1000.  
  1001. # final comment
  1002. </pre>
  1003. <p>When parsed, the above config file produces the following data structure :</p>
  1004. <div class="pysrc"><span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">{</span><br />
  1005.     <span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
  1006.     <span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
  1007.     <span class="pystring">'section 1'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
  1008.         <span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
  1009.         <span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
  1010.         <span class="pystring">'sub-section'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
  1011.             <span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
  1012.             <span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
  1013.             <span class="pystring">'nested section'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
  1014.                 <span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
  1015.                 <span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
  1016.             <span class="pyoperator">}</span><span class="pyoperator">,</span><br />
  1017.         <span class="pyoperator">}</span><span class="pyoperator">,</span><br />
  1018.         <span class="pystring">'sub-section2'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
  1019.             <span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
  1020.             <span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
  1021.         <span class="pyoperator">}</span><span class="pyoperator">,</span><br />
  1022.         <span class="pystring">'sub-section3'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
  1023.             <span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
  1024.             <span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
  1025.         <span class="pyoperator">}</span><span class="pyoperator">,</span><br />
  1026.     <span class="pyoperator">}</span><span class="pyoperator">,</span><br />
  1027. <span class="pyoperator">}</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>Sections are ordered: note how the structure of the resulting ConfigObj is in
  1028. the same order as the original file.</p>
  1029. <div class="note">
  1030. <p class="first admonition-title">Note</p>
  1031. <p>In ConfigObj 4.3.0 <em>empty values</em> became valid syntax. They are read as the
  1032. empty string. There is also an option/attribute (<tt class="docutils literal"><span class="pre">write_empty_values</span></tt>) to
  1033. allow the writing of these.</p>
  1034. <p>This is mainly to support 'legacy' config files, written from other
  1035. applications. This is documented under <a class="reference internal" href="#empty-values">Empty Values</a>.</p>
  1036. <p class="last"><a class="reference internal" href="#unrepr-mode">unrepr mode</a> introduces <em>another</em> syntax variation, used for storing
  1037. basic Python datatypes in config files.  <img src="/smilies/smile.gif" alt="Smile" height="15" width="15" /> </p>
  1038. </div>
  1039. </div>
  1040. <div class="section" id="sections">
  1041. <h1><a class="toc-backref" href="#id65">7   Sections</a></h1>
  1042. <p>Every section in a ConfigObj has certain properties. The ConfigObj itself also
  1043. has these properties, because it too is a section (sometimes called the <em>root
  1044. section</em>).</p>
  1045. <p><tt class="docutils literal"><span class="pre">Section</span></tt> is a subclass of the standard new-class dictionary, therefore it
  1046. has <strong>all</strong> the methods of a normal dictionary. This means you can <tt class="docutils literal"><span class="pre">update</span></tt>
  1047. and <tt class="docutils literal"><span class="pre">clear</span></tt> sections.</p>
  1048. <div class="note">
  1049. <p class="first admonition-title">Note</p>
  1050. <p>You create a new section by assigning a member to be a dictionary.</p>
  1051. <p>The new <tt class="docutils literal"><span class="pre">Section</span></tt> is created <em>from</em> the dictionary, but isn't the same
  1052. thing as the dictionary. (So references to the dictionary you use to create
  1053. the section <em>aren't</em> references to the new section).</p>
  1054. <p>Note the following.</p>
  1055. <div class="pysrc"><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  1056. <span class="pytext">vals</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><span class="pystring">'key1'</span><span class="pyoperator">:</span> <span class="pystring">'value 1'</span><span class="pyoperator">,</span><br />
  1057.         <span class="pystring">'key2'</span><span class="pyoperator">:</span> <span class="pystring">'value 2'</span><br />
  1058.        <span class="pyoperator">}</span><br />
  1059. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'vals'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">vals</span><br />
  1060. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'vals'</span><span class="pyoperator">]</span> <span class="pyoperator">==</span> <span class="pytext">vals</span><br />
  1061. <span class="pytext">True</span><br />
  1062. <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'vals'</span><span class="pyoperator">]</span> <span class="pykeyword">is</span> <span class="pytext">vals</span><br />
  1063. <span class="pytext">False</span><span class="pytext"></span></div><p class="last">If you now change <tt class="docutils literal"><span class="pre">vals</span></tt>, the changes won't be reflected in <tt class="docutils literal"><span class="pre">config['vals']</span></tt>.</p>
  1064. </div>
  1065. <p>A section is ordered, following its <tt class="docutils literal"><span class="pre">scalars</span></tt> and <tt class="docutils literal"><span class="pre">sections</span></tt>
  1066. attributes documented below. This means that the following dictionary
  1067. attributes return their results in order.</p>
  1068. <ul>
  1069. <li><p class="first">'__iter__'</p>
  1070. <blockquote>
  1071. <p>More commonly known as <tt class="docutils literal"><span class="pre">for</span> <span class="pre">member</span> <span class="pre">in</span> <span class="pre">section:</span></tt>.</p>
  1072. </blockquote>
  1073. </li>
  1074. <li><p class="first">'__repr__' and '__str__'</p>
  1075. <blockquote>
  1076. <p>Any time you print or display the ConfigObj.</p>
  1077. </blockquote>
  1078. </li>
  1079. <li><p class="first">'items'</p>
  1080. </li>
  1081. <li><p class="first">'iteritems'</p>
  1082. </li>
  1083. <li><p class="first">'iterkeys'</p>
  1084. </li>
  1085. <li><p class="first">'itervalues'</p>
  1086. </li>
  1087. <li><p class="first">'keys'</p>
  1088. </li>
  1089. <li><p class="first">'popitem'</p>
  1090. </li>
  1091. <li><p class="first">'values'</p>
  1092. </li>
  1093. </ul>
  1094. <div class="section" id="section-attributes">
  1095. <h2><a class="toc-backref" href="#id66">7.1   Section Attributes</a></h2>
  1096. <ul>
  1097. <li><p class="first">main</p>
  1098. <blockquote>
  1099. <p>A reference to the main ConfigObj.</p>
  1100. </blockquote>
  1101. </li>
  1102. <li><p class="first">parent</p>
  1103. <blockquote>
  1104. <p>A reference to the 'parent' section, the section that this section is a
  1105. member of.</p>
  1106. <p>On the ConfigObj this attribute is a reference to itself. You can use this
  1107. to walk up the sections, stopping when <tt class="docutils literal"><span class="pre">section.parent</span> <span class="pre">is</span> <span class="pre">section</span></tt>.</p>
  1108. </blockquote>
  1109. </li>
  1110. <li><p class="first">depth</p>
  1111. <blockquote>
  1112. <p>The nesting level of the current section.</p>
  1113. <p>If you create a new ConfigObj and add sections, 1 will be added to the
  1114. depth level between sections.</p>
  1115. </blockquote>
  1116. </li>
  1117. <li><p class="first">defaults</p>
  1118. <blockquote>
  1119. <p>This attribute is a list of scalars that came from default values. Values
  1120. that came from defaults aren't written out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.
  1121. Setting any of these values in the section removes them from the defaults
  1122. list.</p>
  1123. </blockquote>
  1124. </li>
  1125. <li><p class="first">default_values</p>
  1126. <blockquote>
  1127. <p>This attribute is a dictionary mapping keys to the default values for the
  1128. keys. By default it is an empty dictionary and is populated when you
  1129. validate the ConfigObj.</p>
  1130. </blockquote>
  1131. </li>
  1132. <li><p class="first">scalars, sections</p>
  1133. <blockquote>
  1134. <p>These attributes are normal lists, representing the order that members,
  1135. single values and subsections appear in the section. The order will either
  1136. be the order of the original config file, <em>or</em> the order that you added
  1137. members.</p>
  1138. <p>The order of members in this lists is the order that <tt class="docutils literal"><span class="pre">write</span></tt> creates in
  1139. the config file. The <tt class="docutils literal"><span class="pre">scalars</span></tt> list is output before the <tt class="docutils literal"><span class="pre">sections</span></tt>
  1140. list.</p>
  1141. <p>Adding or removing members also alters these lists. You can manipulate the
  1142. lists directly to alter the order of members.</p>
  1143. <div class="warning">
  1144. <p class="first admonition-title">Warning</p>
  1145. <p class="last">If you alter the <tt class="docutils literal"><span class="pre">scalars</span></tt>, <tt class="docutils literal"><span class="pre">sections</span></tt>, or <tt class="docutils literal"><span class="pre">defaults</span></tt> attributes
  1146. so that they no longer reflect the contents of the section, you will
  1147. break your ConfigObj.</p>
  1148. </div>
  1149. <p>See also the <tt class="docutils literal"><span class="pre">rename</span></tt> method.</p>
  1150. </blockquote>
  1151. </li>
  1152. <li><p class="first">comments</p>
  1153. <blockquote>
  1154. <p>This is a dictionary of comments associated with each member. Each entry is
  1155. a list of lines. These lines are written out before the member.</p>
  1156. </blockquote>
  1157. </li>
  1158. <li><p class="first">inline_comments</p>
  1159. <blockquote>
  1160. <p>This is <em>another</em> dictionary of comments associated with each member. Each
  1161. entry is a string that is put inline with the member.</p>
  1162. </blockquote>
  1163. </li>
  1164. <li><p class="first">configspec</p>
  1165. <blockquote>
  1166. <p>The configspec attribute is a dictionary mapping scalars to <em>checks</em>. A
  1167. check defines the expected type and possibly the allowed values for a
  1168. member.</p>
  1169. <p>The configspec has the same format as a config file, but instead of values
  1170. it has a specification for the value (which may include a default value).
  1171. The <a class="reference internal" href="#validate">validate</a> method uses it to check the config file makes sense. If a
  1172. configspec is passed in when the ConfigObj is created, then it is parsed
  1173. and broken up to become the <tt class="docutils literal"><span class="pre">configspec</span></tt> attribute of each section.</p>
  1174. <p>If you didn't pass in a configspec, this attribute will be <tt class="docutils literal"><span class="pre">None</span></tt> on the
  1175. root section (the main ConfigObj).</p>
  1176. <p>You can set the configspec attribute directly on a section.</p>
  1177. <p>See the <a class="reference internal" href="#validation">validation</a> section for full details of how to write configspecs.</p>
  1178. </blockquote>
  1179. </li>
  1180. </ul>
  1181. </div>
  1182. <div class="section" id="section-methods">
  1183. <h2><a class="toc-backref" href="#id67">7.2   Section Methods</a></h2>
  1184. <ul>
  1185. <li><p class="first"><strong>dict</strong></p>
  1186. <blockquote>
  1187. <p>This method takes no arguments. It returns a deep copy of the section as a
  1188. dictionary. All subsections will also be dictionaries, and list values will
  1189. be copies, rather than references to the original <a class="footnote-reference" href="#id25" id="id11">[10]</a>.</p>
  1190. </blockquote>
  1191. </li>
  1192. <li><p class="first"><strong>rename</strong></p>
  1193. <blockquote>
  1194. <p><tt class="docutils literal"><span class="pre">rename(oldkey,</span> <span class="pre">newkey)</span></tt></p>
  1195. <p>This method renames a key, without affecting its position in the sequence.</p>
  1196. <p>It is mainly implemented for the <tt class="docutils literal"><span class="pre">encode</span></tt> and <tt class="docutils literal"><span class="pre">decode</span></tt> methods, which
  1197. provide some Unicode support.</p>
  1198. </blockquote>
  1199. </li>
  1200. <li><p class="first"><strong>merge</strong></p>
  1201. <blockquote>
  1202. <p><tt class="docutils literal"><span class="pre">merge(indict)</span></tt></p>
  1203. <p>This method is a <em>recursive update</em> method. It allows you to merge two
  1204. config files together.</p>
  1205. <p>You would typically use this to create a default ConfigObj and then merge
  1206. in user settings. This way users only need to specify values that are
  1207. different from the default.</p>
  1208. <p>For example :</p>
  1209. <div class="pysrc"><span class="pycomment"># def_cfg contains your default config settings<br />
  1210. </span><span class="pycomment"># user_cfg contains the user settings<br />
  1211. </span><span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">def_cfg</span><span class="pyoperator">)</span><br />
  1212. <span class="pytext">usr</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">user_cfg</span><span class="pyoperator">)</span><br />
  1213. <span class="pycomment">#<br />
  1214. </span><span class="pytext">cfg</span><span class="pyoperator">.</span><span class="pytext">merge</span><span class="pyoperator">(</span><span class="pytext">usr</span><span class="pyoperator">)</span><br />
  1215. <br />
  1216. <span class="pystring">"""<br />
  1217. cfg now contains a combination of the default settings and the user<br />
  1218. settings.<br />
  1219. <br />
  1220. The user settings will have overwritten any of the default ones.<br />
  1221. """</span><span class="pytext"></span></div></blockquote>
  1222. </li>
  1223. <li><p class="first"><strong>walk</strong></p>
  1224. <blockquote>
  1225. <p>This method can be used to transform values and names. See <a class="reference internal" href="#walking-a-section">walking a
  1226. section</a> for examples and explanation.</p>
  1227. </blockquote>
  1228. </li>
  1229. <li><p class="first"><strong>decode</strong></p>
  1230. <blockquote>
  1231. <p><tt class="docutils literal"><span class="pre">decode(encoding)</span></tt></p>
  1232. <p>This method decodes names and values into Unicode objects, using the
  1233. supplied encoding.</p>
  1234. </blockquote>
  1235. </li>
  1236. <li><p class="first"><strong>encode</strong></p>
  1237. <blockquote>
  1238. <p><tt class="docutils literal"><span class="pre">encode(encoding)</span></tt></p>
  1239. <p>This method is the opposite of <tt class="docutils literal"><span class="pre">decode</span></tt>  <img src="/smilies/exclaim.gif" alt="Exclamation" height="15" width="15" /> .</p>
  1240. <p>It encodes names and values using the supplied encoding. If any of your
  1241. names/values are strings rather than Unicode, Python will have to do an
  1242. implicit decode first. (This method uses <tt class="docutils literal"><span class="pre">sys.defaultencoding</span></tt> for
  1243. implicit decodes.)</p>
  1244. </blockquote>
  1245. </li>
  1246. <li><p class="first"><strong>as_bool</strong></p>
  1247. <blockquote>
  1248. <p><tt class="docutils literal"><span class="pre">as_bool(key)</span></tt></p>
  1249. <p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the key contains a string that represents <tt class="docutils literal"><span class="pre">True</span></tt>, or
  1250. is the <tt class="docutils literal"><span class="pre">True</span></tt> object.</p>
  1251. <p>Returns <tt class="docutils literal"><span class="pre">False</span></tt> if the key contains a string that represents <tt class="docutils literal"><span class="pre">False</span></tt>,
  1252. or is the <tt class="docutils literal"><span class="pre">False</span></tt> object.</p>
  1253. <p>Raises a <tt class="docutils literal"><span class="pre">ValueError</span></tt> if the key contains anything else.</p>
  1254. <p>Strings that represent <tt class="docutils literal"><span class="pre">True</span></tt> are (not case sensitive) :</p>
  1255. <pre class="literal-block">
  1256. true, yes, on, 1
  1257. </pre>
  1258. <p>Strings that represent <tt class="docutils literal"><span class="pre">False</span></tt> are :</p>
  1259. <pre class="literal-block">
  1260. false, no, off, 0
  1261. </pre>
  1262. <div class="note">
  1263. <p class="first admonition-title">Note</p>
  1264. <p class="last">In ConfigObj 4.1.0, this method was called <tt class="docutils literal"><span class="pre">istrue</span></tt>. That method is
  1265. now deprecated and will issue a warning when used. It will go away
  1266. in a future release.</p>
  1267. </div>
  1268. </blockquote>
  1269. </li>
  1270. <li><p class="first"><strong>as_int</strong></p>
  1271. <blockquote>
  1272. <p><tt class="docutils literal"><span class="pre">as_int(key)</span></tt></p>
  1273. <p>This returns the value contained in the specified key as an integer.</p>
  1274. <p>It raises a <tt class="docutils literal"><span class="pre">ValueError</span></tt> if the conversion can't be done.</p>
  1275. </blockquote>
  1276. </li>
  1277. <li><p class="first"><strong>as_float</strong></p>
  1278. <blockquote>
  1279. <p><tt class="docutils literal"><span class="pre">as_float(key)</span></tt></p>
  1280. <p>This returns the value contained in the specified key as a float.</p>
  1281. <p>It raises a <tt class="docutils literal"><span class="pre">ValueError</span></tt> if the conversion can't be done.</p>
  1282. </blockquote>
  1283. </li>
  1284. <li><p class="first"><strong>restore_default</strong></p>
  1285. <blockquote>
  1286. <p><tt class="docutils literal"><span class="pre">restore_default(key)</span></tt></p>
  1287. <p>Restore (and return) the default value for the specified key.</p>
  1288. <p>This method will only work for a ConfigObj that was created
  1289. with a configspec and has been validated.</p>
  1290. <p>If there is no default value for this key, <tt class="docutils literal"><span class="pre">KeyError</span></tt> is raised.</p>
  1291. </blockquote>
  1292. </li>
  1293. <li><p class="first"><strong>restore_defaults</strong></p>
  1294. <blockquote>
  1295. <p><tt class="docutils literal"><span class="pre">restore_defaults()</span></tt></p>
  1296. <p>Recursively restore default values to all members
  1297. that have them.</p>
  1298. <p>This method will only work for a ConfigObj that was created
  1299. with a configspec and has been validated.</p>
  1300. <p>It doesn't delete or modify entries without default values.</p>
  1301. </blockquote>
  1302. </li>
  1303. </ul>
  1304. </div>
  1305. <div class="section" id="walking-a-section">
  1306. <h2><a class="toc-backref" href="#id68">7.3   Walking a Section</a></h2>
  1307. <div class="note">
  1308. <p class="first admonition-title">Note</p>
  1309. <p class="last">The walk method allows you to call a function on every member/name.</p>
  1310. </div>
  1311. <div class="pysrc"><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">function</span><span class="pyoperator">,</span> <span class="pytext">raise_errors</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">,</span><br />
  1312.         <span class="pytext">call_on_sections</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">,</span> <span class="pyoperator">**</span><span class="pytext">keywargs</span><span class="pyoperator">)</span><span class="pyoperator">:</span><span class="pytext"></span></div><p><tt class="docutils literal"><span class="pre">walk</span></tt> is a method of the <tt class="docutils literal"><span class="pre">Section</span></tt> object. This means it is also a method
  1313. of ConfigObj.</p>
  1314. <p>It walks through every member and calls a function on the keyword and value. It
  1315. walks recursively through subsections.</p>
  1316. <p>It returns a dictionary of all the computed values.</p>
  1317. <p>If the function raises an exception, the default is to propagate the error, and
  1318. stop. If <tt class="docutils literal"><span class="pre">raise_errors=False</span></tt> then it sets the return value for that keyword
  1319. to <tt class="docutils literal"><span class="pre">False</span></tt> instead, and continues. This is similar to the way <a class="reference internal" href="#validation">validation</a>
  1320. works.</p>
  1321. <p>Your function receives the arguments <tt class="docutils literal"><span class="pre">(section,</span> <span class="pre">key)</span></tt>. The current value is
  1322. then <tt class="docutils literal"><span class="pre">section[key]</span></tt> <a class="footnote-reference" href="#id26" id="id12">[11]</a>. Any unrecognised keyword arguments you pass to
  1323. walk, are passed on to the function.</p>
  1324. <p>Normally <tt class="docutils literal"><span class="pre">walk</span></tt> just recurses into subsections. If you are transforming (or
  1325. checking) names as well as values, then you want to be able to change the names
  1326. of sections. In this case set <tt class="docutils literal"><span class="pre">call_on_sections</span></tt> to <tt class="docutils literal"><span class="pre">True</span></tt>. Now, on
  1327. encountering a sub-section, <em>first</em> the function is called for the <em>whole</em>
  1328. sub-section, and <em>then</em> it recurses into it's members. This means your function
  1329. must be able to handle receiving dictionaries as well as strings and lists.</p>
  1330. <p>If you are using the return value from <tt class="docutils literal"><span class="pre">walk</span></tt> <em>and</em> <tt class="docutils literal"><span class="pre">call_on_sections</span></tt>,
  1331. note that walk discards the return value when it calls your function.</p>
  1332. <div class="caution">
  1333. <p class="first admonition-title">Caution!</p>
  1334. <p class="last">You can use <tt class="docutils literal"><span class="pre">walk</span></tt> to transform the names of members of a section
  1335. but you mustn't add or delete members.</p>
  1336. </div>
  1337. </div>
  1338. <div class="section" id="examples">
  1339. <h2><a class="toc-backref" href="#id69">7.4   Examples</a></h2>
  1340. <p>Examples that use the walk method are the <tt class="docutils literal"><span class="pre">encode</span></tt> and <tt class="docutils literal"><span class="pre">decode</span></tt> methods.
  1341. They both define a function and pass it to walk. Because these functions
  1342. transform names as well as values (from byte strings to Unicode) they set
  1343. <tt class="docutils literal"><span class="pre">call_on_sections=True</span></tt>.</p>
  1344. <p>To see how they do it, <em>read the source Luke</em>  <img src="/smilies/cool.gif" alt="Cool" height="15" width="15" /> .</p>
  1345. <p>You can use this for transforming all values in your ConfigObj. For example
  1346. you might like the nested lists from ConfigObj 3. This was provided by the
  1347. <a class="reference external" href="http://www.voidspace.org.uk/python/modules.shtml#listquote">listquote</a> module. You could switch off the parsing for list values
  1348. (<tt class="docutils literal"><span class="pre">list_values=False</span></tt>) and use listquote to parse every value.</p>
  1349. <p>Another thing you might want to do is use the Python escape codes in your
  1350. values. You might be <em>used</em> to using <tt class="docutils literal"><span class="pre">\n</span></tt> for line feed and <tt class="docutils literal"><span class="pre">\t</span></tt> for tab.
  1351. Obviously we'd need to decode strings that come from the config file (using the
  1352. escape codes). Before writing out we'll need to put the escape codes back in
  1353. encode.</p>
  1354. <p>As an example we'll write a function to use with walk, that encodes or decodes
  1355. values using the <tt class="docutils literal"><span class="pre">string-escape</span></tt> codec.</p>
  1356. <p>The function has to take each value and set the new value. As a bonus we'll
  1357. create one function that will do decode <em>or</em> encode depending on a keyword
  1358. argument.</p>
  1359. <p>We don't want to work with section names, we're only transforming values, so
  1360. we can leave <tt class="docutils literal"><span class="pre">call_on_sections</span></tt> as <tt class="docutils literal"><span class="pre">False</span></tt>. This means the two datatypes we
  1361. have to handle are strings and lists, we can ignore everything else. (We'll
  1362. treat tuples as lists as well).</p>
  1363. <p>We're not using the return values, so it doesn't need to return anything, just
  1364. change the values if appropriate.</p>
  1365. <div class="pysrc"><span class="pykeyword">def</span> <span class="pytext">string_escape</span><span class="pyoperator">(</span><span class="pytext">section</span><span class="pyoperator">,</span> <span class="pytext">key</span><span class="pyoperator">,</span> <span class="pytext">encode</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1366.     <span class="pystring">"""<br />
  1367.     A function to encode or decode using the 'string-escape' codec.<br />
  1368.     To be passed to the walk method of a ConfigObj.<br />
  1369.     By default it decodes.<br />
  1370.     To encode, pass in the keyword argument ``encode=True``.<br />
  1371.     """</span><br />
  1372.     <span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span><br />
  1373.     <span class="pycomment"># is it a type we can work with<br />
  1374. </span>    <span class="pycomment"># NOTE: for platforms where Python > 2.2<br />
  1375. </span>    <span class="pycomment"># you can use basestring instead of (str, unicode)<br />
  1376. </span>    <span class="pykeyword">if</span> <span class="pykeyword">not</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">str</span><span class="pyoperator">,</span> <span class="pytext">unicode</span><span class="pyoperator">,</span> <span class="pytext">list</span><span class="pyoperator">,</span> <span class="pytext">tuple</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1377.         <span class="pycomment"># no !<br />
  1378. </span>        <span class="pykeyword">return</span><br />
  1379.     <span class="pykeyword">elif</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">str</span><span class="pyoperator">,</span> <span class="pytext">unicode</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1380.         <span class="pycomment"># it's a string !<br />
  1381. </span>        <span class="pykeyword">if</span> <span class="pykeyword">not</span> <span class="pytext">encode</span><span class="pyoperator">:</span><br />
  1382.             <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">val</span><span class="pyoperator">.</span><span class="pytext">decode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><br />
  1383.         <span class="pykeyword">else</span><span class="pyoperator">:</span><br />
  1384.             <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">val</span><span class="pyoperator">.</span><span class="pytext">encode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><br />
  1385.     <span class="pykeyword">else</span><span class="pyoperator">:</span><br />
  1386.         <span class="pycomment"># it must be a list or tuple!<br />
  1387. </span>        <span class="pycomment"># we'll be lazy and create a new list<br />
  1388. </span>        <span class="pytext">newval</span> <span class="pyoperator">=</span> <span class="pyoperator">[</span><span class="pyoperator">]</span><br />
  1389.         <span class="pycomment"># we'll check every member of the list<br />
  1390. </span>        <span class="pykeyword">for</span> <span class="pytext">entry</span> <span class="pykeyword">in</span> <span class="pytext">val</span><span class="pyoperator">:</span><br />
  1391.             <span class="pykeyword">if</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">str</span><span class="pyoperator">,</span> <span class="pytext">unicode</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1392.                 <span class="pykeyword">if</span> <span class="pykeyword">not</span> <span class="pytext">encode</span><span class="pyoperator">:</span><br />
  1393.                     <span class="pytext">newval</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">.</span><span class="pytext">decode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><span class="pyoperator">)</span><br />
  1394.                 <span class="pykeyword">else</span><span class="pyoperator">:</span><br />
  1395.                    <span class="pytext">newval</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">.</span><span class="pytext">encode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><span class="pyoperator">)</span><br />
  1396.             <span class="pykeyword">else</span><span class="pyoperator">:</span><br />
  1397.                 <span class="pytext">newval</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">)</span><br />
  1398.         <span class="pycomment"># done !<br />
  1399. </span>        <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span> <span class="pyoperator">=</span>  <span class="pytext">newval</span><br />
  1400. <br />
  1401. <span class="pycomment"># assume we have a ConfigObj called ``config``<br />
  1402. </span><span class="pycomment">#<br />
  1403. </span><span class="pycomment"># To decode<br />
  1404. </span><span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">string_escape</span><span class="pyoperator">)</span><br />
  1405. <span class="pycomment">#<br />
  1406. </span><span class="pycomment"># To encode.<br />
  1407. </span><span class="pycomment"># Because ``walk`` doesn't recognise the ``encode`` argument<br />
  1408. </span><span class="pycomment"># it passes it to our function.<br />
  1409. </span><span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">string_escape</span><span class="pyoperator">,</span> <span class="pytext">encode</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>Here's a simple example of using <tt class="docutils literal"><span class="pre">walk</span></tt> to transform names and values. One
  1410. usecase of this would be to create a <em>standard</em> config file with placeholders
  1411. for section and keynames. You can then use walk to create new config files
  1412. and change values and member names :</p>
  1413. <div class="pysrc"><span class="pycomment"># We use 'XXXX' as a placeholder<br />
  1414. </span><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pystring">'''<br />
  1415. XXXXkey1 = XXXXvalue1<br />
  1416. XXXXkey2 = XXXXvalue2<br />
  1417. XXXXkey3 = XXXXvalue3<br />
  1418. [XXXXsection1]<br />
  1419. XXXXkey1 = XXXXvalue1<br />
  1420. XXXXkey2 = XXXXvalue2<br />
  1421. XXXXkey3 = XXXXvalue3<br />
  1422. [XXXXsection2]<br />
  1423. XXXXkey1 = XXXXvalue1<br />
  1424. XXXXkey2 = XXXXvalue2<br />
  1425. XXXXkey3 = XXXXvalue3<br />
  1426.     [[XXXXsection1]]<br />
  1427.     XXXXkey1 = XXXXvalue1<br />
  1428.     XXXXkey2 = XXXXvalue2<br />
  1429.     XXXXkey3 = XXXXvalue3<br />
  1430. '''</span><span class="pyoperator">.</span><span class="pytext">splitlines</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  1431. <span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">config</span><span class="pyoperator">)</span><br />
  1432. <span class="pycomment">#<br />
  1433. </span><span class="pykeyword">def</span> <span class="pytext">transform</span><span class="pyoperator">(</span><span class="pytext">section</span><span class="pyoperator">,</span> <span class="pytext">key</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1434.     <span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span><br />
  1435.     <span class="pytext">newkey</span> <span class="pyoperator">=</span> <span class="pytext">key</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">'XXXX'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1'</span><span class="pyoperator">)</span><br />
  1436.     <span class="pytext">section</span><span class="pyoperator">.</span><span class="pytext">rename</span><span class="pyoperator">(</span><span class="pytext">key</span><span class="pyoperator">,</span> <span class="pytext">newkey</span><span class="pyoperator">)</span><br />
  1437.     <span class="pykeyword">if</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">tuple</span><span class="pyoperator">,</span> <span class="pytext">list</span><span class="pyoperator">,</span> <span class="pytext">dict</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1438.         <span class="pykeyword">pass</span><br />
  1439.     <span class="pykeyword">else</span><span class="pyoperator">:</span><br />
  1440.         <span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">val</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">'XXXX'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1'</span><span class="pyoperator">)</span><br />
  1441.         <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">newkey</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">val</span><br />
  1442. <span class="pycomment">#<br />
  1443. </span><span class="pytext">cfg</span><span class="pyoperator">.</span><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">transform</span><span class="pyoperator">,</span> <span class="pytext">call_on_sections</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><br />
  1444. <span class="pykeyword">print</span> <span class="pytext">cfg</span><br />
  1445. <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span><br />
  1446. <span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">,</span><br />
  1447. <span class="pystring">'CLIENT1section1'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span><br />
  1448.     <span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">}</span><span class="pyoperator">,</span><br />
  1449. <span class="pystring">'CLIENT1section2'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span><br />
  1450.     <span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">,</span><br />
  1451.     <span class="pystring">'CLIENT1section1'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span><br />
  1452.         <span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">}</span><span class="pyoperator">}</span><span class="pyoperator">}</span><span class="pyoperator">)</span><span class="pytext"></span></div></div>
  1453. </div>
  1454. <div class="section" id="exceptions">
  1455. <h1><a class="toc-backref" href="#id70">8   Exceptions</a></h1>
  1456. <p>There are several places where ConfigObj may raise exceptions (other than
  1457. because of bugs).</p>
  1458. <ol class="arabic">
  1459. <li><dl class="first docutils">
  1460. <dt>If a configspec filename you pass in doesn't exist, or a config file</dt>
  1461. <dd><p class="first last">filename doesn't exist <em>and</em> <tt class="docutils literal"><span class="pre">file_error=True</span></tt>, an <tt class="docutils literal"><span class="pre">IOError</span></tt> will be
  1462. raised.</p>
  1463. </dd>
  1464. </dl>
  1465. </li>
  1466. <li><dl class="first docutils">
  1467. <dt>If you try to set a non-string key, or a non string value when</dt>
  1468. <dd><p class="first last"><tt class="docutils literal"><span class="pre">stringify=False</span></tt>, a <tt class="docutils literal"><span class="pre">TypeError</span></tt> will be raised.</p>
  1469. </dd>
  1470. </dl>
  1471. </li>
  1472. <li><p class="first">A badly built config file will cause parsing errors.</p>
  1473. </li>
  1474. <li><p class="first">A parsing error can also occur when reading a configspec.</p>
  1475. </li>
  1476. <li><dl class="first docutils">
  1477. <dt>In string interpolation you can specify a value that doesn't exist, or</dt>
  1478. <dd><p class="first last">create circular references (recursion).</p>
  1479. </dd>
  1480. </dl>
  1481. </li>
  1482. <li><dl class="first docutils">
  1483. <dt>If you have a <tt class="docutils literal"><span class="pre">__many__</span></tt> repeated section with other section definitions</dt>
  1484. <dd><p class="first last">(in a configspec), a <tt class="docutils literal"><span class="pre">RepeatSectionError</span></tt> will be raised.</p>
  1485. </dd>
  1486. </dl>
  1487. </li>
  1488. </ol>
  1489. <dl class="docutils">
  1490. <dt>Number 5 (which is actually two different types of exceptions) is documented</dt>
  1491. <dd>in <a class="reference internal" href="#string-interpolation">String Interpolation</a>.</dd>
  1492. </dl>
  1493. <p>Number 6 is explained in the <a class="reference internal" href="#validation">validation</a> section.</p>
  1494. <p><em>This</em> section is about errors raised during parsing.</p>
  1495. <p>The base error class is <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>. This is a subclass of
  1496. <tt class="docutils literal"><span class="pre">SyntaxError</span></tt>, so you can trap for <tt class="docutils literal"><span class="pre">SyntaxError</span></tt> without needing to
  1497. directly import any of the ConfigObj exceptions.</p>
  1498. <p>The following other exceptions are defined (all deriving from
  1499. <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>) :</p>
  1500. <ul>
  1501. <li><p class="first"><tt class="docutils literal"><span class="pre">NestingError</span></tt></p>
  1502. <blockquote>
  1503. <p>This error indicates either a mismatch in the brackets in a section marker,
  1504. or an excessive level of nesting.</p>
  1505. </blockquote>
  1506. </li>
  1507. <li><p class="first"><tt class="docutils literal"><span class="pre">ParseError</span></tt></p>
  1508. <blockquote>
  1509. <p>This error indicates that a line is badly written. It is neither a valid
  1510. <tt class="docutils literal"><span class="pre">key</span> <span class="pre">=</span> <span class="pre">value</span></tt> line, nor a valid section marker line, nor a comment line.</p>
  1511. </blockquote>
  1512. </li>
  1513. <li><p class="first"><tt class="docutils literal"><span class="pre">DuplicateError</span></tt></p>
  1514. <blockquote>
  1515. <p>The keyword or section specified already exists.</p>
  1516. </blockquote>
  1517. </li>
  1518. <li><p class="first"><tt class="docutils literal"><span class="pre">ConfigspecError</span></tt></p>
  1519. <blockquote>
  1520. <p>An error occurred whilst parsing a configspec.</p>
  1521. </blockquote>
  1522. </li>
  1523. <li><p class="first"><tt class="docutils literal"><span class="pre">UnreprError</span></tt></p>
  1524. <blockquote>
  1525. <p>An error occurred when parsing a value in <a class="reference internal" href="#unrepr-mode">unrepr mode</a>.</p>
  1526. </blockquote>
  1527. </li>
  1528. <li><p class="first"><tt class="docutils literal"><span class="pre">ReloadError</span></tt></p>
  1529. <blockquote>
  1530. <p><tt class="docutils literal"><span class="pre">reload</span></tt> was called on a ConfigObj instance that doesn't have a valid
  1531. filename attribute.</p>
  1532. </blockquote>
  1533. </li>
  1534. </ul>
  1535. <p>When parsing a configspec, ConfigObj will stop on the first error it
  1536. encounters.  It will raise a <tt class="docutils literal"><span class="pre">ConfigspecError</span></tt>. This will have an <tt class="docutils literal"><span class="pre">error</span></tt>
  1537. attribute, which is the actual error that was raised.</p>
  1538. <p>Behaviour when parsing a config file depends on the option <tt class="docutils literal"><span class="pre">raise_errors</span></tt>.
  1539. If ConfigObj encounters an error while parsing a config file:</p>
  1540. <blockquote>
  1541. <p>If <tt class="docutils literal"><span class="pre">raise_errors=True</span></tt> then ConfigObj will raise the appropriate error
  1542. and parsing will stop.</p>
  1543. <p>If <tt class="docutils literal"><span class="pre">raise_errors=False</span></tt> (the default) then parsing will continue to the
  1544. end and <em>all</em> errors will be collected.</p>
  1545. </blockquote>
  1546. <p>If <tt class="docutils literal"><span class="pre">raise_errors</span></tt> is False and multiple errors are found a <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>
  1547. is raised. The error raised has a <tt class="docutils literal"><span class="pre">config</span></tt> attribute, which is the parts of
  1548. the ConfigObj that parsed successfully. It also has an attribute <tt class="docutils literal"><span class="pre">errors</span></tt>,
  1549. which is a list of <em>all</em> the errors raised. Each entry in the list is an
  1550. instance of the appropriate error type. Each one has the following attributes
  1551. (useful for delivering a sensible error message to your user) :</p>
  1552. <ul class="simple">
  1553. <li><tt class="docutils literal"><span class="pre">line</span></tt>: the original line that caused the error.</li>
  1554. <li><tt class="docutils literal"><span class="pre">line_number</span></tt>: its number in the config file.</li>
  1555. <li><tt class="docutils literal"><span class="pre">message</span></tt>: the error message that accompanied the error.</li>
  1556. </ul>
  1557. <p>If only one error is found, then that error is re-raised. The error still has
  1558. the <tt class="docutils literal"><span class="pre">config</span></tt> and <tt class="docutils literal"><span class="pre">errors</span></tt> attributes. This means that your error handling
  1559. code can be the same whether one error is raised in parsing , or several.</p>
  1560. <p>It also means that in the most common case (a single error) a useful error
  1561. message will be raised.</p>
  1562. <div class="note">
  1563. <p class="first admonition-title">Note</p>
  1564. <p class="last">One wrongly written line could break the basic structure of your config
  1565. file. This could cause every line after it to flag an error, so having a
  1566. list of all the lines that caused errors may not be as useful as it sounds.
  1567.  <img src="/smilies/sad.gif" alt="Sad" height="15" width="15" /> .</p>
  1568. </div>
  1569. </div>
  1570. <div class="section" id="validation">
  1571. <h1><a class="toc-backref" href="#id71">9   Validation</a></h1>
  1572. <div class="hint">
  1573. <p class="first admonition-title">Hint</p>
  1574. <p class="last">The system of configspecs can seem confusing at first, but is actually
  1575. quite simple and powerful. For a concrete example of how to use it, you may
  1576. find this blog entry helpful :
  1577. <a class="reference external" href="http://www.voidspace.org.uk/python/weblog/arch_d7_2006_03_04.shtml#e257">Transforming Values with ConfigObj</a>.</p>
  1578. </div>
  1579. <p>Validation is done through a combination of the <a class="reference internal" href="#configspec">configspec</a> and a <tt class="docutils literal"><span class="pre">Validator</span></tt>
  1580. object. For this you need <em>validate.py</em> <a class="footnote-reference" href="#id27" id="id14">[12]</a>. See <a class="reference internal" href="#downloading">downloading</a> if you don't
  1581. have a copy.</p>
  1582. <p>Validation can perform two different operations :</p>
  1583. <ol class="arabic">
  1584. <li><dl class="first docutils">
  1585. <dt>Check that a value meets a specification. For example, check that a value</dt>
  1586. <dd><p class="first last">is an integer between one and six, or is a choice from a specific set of
  1587. options.</p>
  1588. </dd>
  1589. </dl>
  1590. </li>
  1591. <li><dl class="first docutils">
  1592. <dt>It can convert the value into the type required. For example, if one of</dt>
  1593. <dd><p class="first last">your values is a port number, validation will turn it into an integer for
  1594. you.</p>
  1595. </dd>
  1596. </dl>
  1597. </li>
  1598. </ol>
  1599. <p>So validation can act as a transparent layer between the datatypes of your
  1600. application configuration (boolean, integers, floats, etc) and the text format
  1601. of your config file.</p>
  1602. <div class="section" id="configspec">
  1603. <h2><a class="toc-backref" href="#id72">9.1   configspec</a></h2>
  1604. <p>The <tt class="docutils literal"><span class="pre">validate</span></tt> method checks members against an entry in the configspec. Your
  1605. configspec therefore resembles your config file, with a check for every member.</p>
  1606. <p>In order to perform validation you need a <tt class="docutils literal"><span class="pre">Validator</span></tt> object. This has
  1607. several useful built-in check functions. You can also create your own custom
  1608. functions and register them with your Validator object.</p>
  1609. <p>Each check is the name of one of these functions, including any parameters and
  1610. keyword arguments. The configspecs look like function calls, and they map to
  1611. function calls.</p>
  1612. <p>The basic datatypes that an un-extended Validator can test for are :</p>
  1613. <ul class="simple">
  1614. <li>boolean values (True and False)</li>
  1615. <li>integers (including minimum and maximum values)</li>
  1616. <li>floats (including min and max)</li>
  1617. <li>strings (including min and max length)</li>
  1618. <li>IP addresses (v4 only)</li>
  1619. </ul>
  1620. <p>It can also handle lists of these types and restrict a value to being one from
  1621. a set of options.</p>
  1622. <p>An example configspec is going to look something like :</p>
  1623. <pre class="literal-block">
  1624. port = integer(0, 100)
  1625. user = string(max=25)
  1626. mode = option('quiet', 'loud', 'silent')
  1627. </pre>
  1628. <p>You can specify default values, and also have the same configspec applied to
  1629. several sections. This is called <a class="reference internal" href="#repeated-sections">repeated sections</a>.</p>
  1630. <p>For full details on writing configspecs, please refer to the <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">validate.py
  1631. documentation</a>.</p>
  1632. <div class="important">
  1633. <p class="first admonition-title">Important</p>
  1634. <p>Your configspec is read by ConfigObj in the same way as a config file.</p>
  1635. <p>That means you can do interpolation <em>within</em> your configspec.</p>
  1636. <p class="last">In order to allow this, checks in the 'DEFAULT' section (of the root level
  1637. of your configspec) are <em>not</em> used.</p>
  1638. </div>
  1639. <p>If you need to specify the encoding of your configspec, then you can pass in a
  1640. ConfigObj instance as your configspec. When you read your configspec file, you
  1641. <em>must</em> specify <tt class="docutils literal"><span class="pre">list_values=False</span></tt>.</p>
  1642. <div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
  1643. <span class="pytext">configspec</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">configspecfilename</span><span class="pyoperator">,</span> <span class="pytext">encoding</span><span class="pyoperator">=</span><span class="pystring">'UTF8'</span><span class="pyoperator">,</span><br />
  1644.     <span class="pytext">list_values</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">)</span><br />
  1645. <span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">configspec</span><span class="pyoperator">)</span><span class="pytext"></span></div></div>
  1646. <div class="section" id="type-conversion">
  1647. <h2><a class="toc-backref" href="#id73">9.2   Type Conversion</a></h2>
  1648. <p>By default, validation does type conversion. This means that if you specify
  1649. <tt class="docutils literal"><span class="pre">integer</span></tt> as the check, then calling <a class="reference internal" href="#validate">validate</a> will actually change the value
  1650. to an integer (so long as the check succeeds).</p>
  1651. <p>It also means that when you call the <a class="reference internal" href="#write">write</a> method, the value will be converted
  1652. back into a string using the <tt class="docutils literal"><span class="pre">str</span></tt> function.</p>
  1653. <p>To switch this off, and leave values as strings after validation, you need to
  1654. set the <a class="reference internal" href="#stringify">stringify</a> attribute to <tt class="docutils literal"><span class="pre">False</span></tt>. If this is the case, attempting to
  1655. set a value to a non-string will raise an error.</p>
  1656. </div>
  1657. <div class="section" id="default-values">
  1658. <h2><a class="toc-backref" href="#id74">9.3   Default Values</a></h2>
  1659. <p>You can set a default value in your check. If the value is missing from the
  1660. config file then this value will be used instead. This means that your user
  1661. only has to supply values that differ from the defaults.</p>
  1662. <p>If you <em>don't</em> supply a default then for a value to be missing is an error,
  1663. and this will show in the <a class="reference internal" href="#return-value">return value</a> from validate.</p>
  1664. <p>Additionally you can set the default to be <tt class="docutils literal"><span class="pre">None</span></tt>. This means the value will
  1665. be set to <tt class="docutils literal"><span class="pre">None</span></tt> (the object) <em>whichever check is used</em>. (It will be set to
  1666. <tt class="docutils literal"><span class="pre">''</span></tt> rather than <tt class="docutils literal"><span class="pre">None</span></tt> if <a class="reference internal" href="#stringify">stringify</a> is <tt class="docutils literal"><span class="pre">False</span></tt>). You can use this
  1667. to easily implement optional values in your config files.</p>
  1668. <pre class="literal-block">
  1669. port = integer(0, 100, default=80)
  1670. user = string(max=25, default=0)
  1671. mode = option('quiet', 'loud', 'silent', default='loud')
  1672. nick = string(default=None)
  1673. </pre>
  1674. <div class="note">
  1675. <p class="first admonition-title">Note</p>
  1676. <p>Because the default goes through type conversion, it also has to pass the
  1677. check.</p>
  1678. <p class="last">Note that <tt class="docutils literal"><span class="pre">default=None</span></tt> is case sensitive.</p>
  1679. </div>
  1680. <div class="section" id="id15">
  1681. <h3><a class="toc-backref" href="#id75">9.3.1   List Values</a></h3>
  1682. <p>It's possible that you will want to specify a list as a default value. To avoid
  1683. confusing syntax with commas and quotes you use a list constructor to specify
  1684. that keyword arguments are lists. This includes the <tt class="docutils literal"><span class="pre">default</span></tt> value. This
  1685. makes checks look something like :</p>
  1686. <pre class="literal-block">
  1687. checkname(default=list('val1', 'val2', 'val3'))
  1688. </pre>
  1689. <p>This works with all keyword arguments, but is most useful for default values.</p>
  1690. </div>
  1691. </div>
  1692. <div class="section" id="repeated-sections">
  1693. <h2><a class="toc-backref" href="#id76">9.4   Repeated Sections</a></h2>
  1694. <p>Repeated sections are a way of specifying a configspec for a section that
  1695. should be applied to <em>all</em> subsections in the same section.</p>
  1696. <p>The easiest way of explaining this is to give an example. Suppose you have a
  1697. config file that describes a dog. That dog has various attributes, but it can
  1698. also have many fleas. You don't know in advance how many fleas there will be,
  1699. or what they will be called, but you want each flea validated against the same
  1700. configspec.</p>
  1701. <p>We can define a section called <em>fleas</em>. We want every flea in that section
  1702. (every sub-section) to have the same configspec applied to it. We do this by
  1703. defining a single section called <tt class="docutils literal"><span class="pre">__many__</span></tt>.</p>
  1704. <pre class="literal-block">
  1705. [dog]
  1706. name = string(default=Rover)
  1707. age = float(0, 99, default=0)
  1708.  
  1709.     [[fleas]]
  1710.  
  1711.         [[[__many__]]]
  1712.         bloodsucker = boolean(default=True)
  1713.         children = integer(default=10000)
  1714.         size = option(small, tiny, micro, default=tiny)
  1715. </pre>
  1716. <p>Every flea on our dog will now be validated using the <tt class="docutils literal"><span class="pre">__many__</span></tt> configspec.</p>
  1717. <p>If you define another sub-section in a section <em>as well as</em> a <tt class="docutils literal"><span class="pre">__many__</span></tt> then
  1718. you will get an error.</p>
  1719. <p><tt class="docutils literal"><span class="pre">__many__</span></tt> sections can have sub-sections, including their own <tt class="docutils literal"><span class="pre">__many__</span></tt>
  1720. sub-sections. Defaults work in the normal way in repeated sections.</p>
  1721. </div>
  1722. <div class="section" id="copy-mode">
  1723. <h2><a class="toc-backref" href="#id77">9.5   Copy Mode</a></h2>
  1724. <p>Because you can specify default values in your configspec, you can use
  1725. ConfigObj to write out default config files for your application.</p>
  1726. <p>However, normally values supplied from a default in a configspec are <em>not</em>
  1727. written out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.</p>
  1728. <p>To do this, you need to specify <tt class="docutils literal"><span class="pre">copy=True</span></tt> when you call validate. As well
  1729. as not marking values as default, all the comments in the configspec file
  1730. will be copied into your ConfigObj instance.</p>
  1731. <div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
  1732. <span class="pykeyword">from</span> <span class="pytext">validate</span> <span class="pykeyword">import</span> <span class="pytext">Validator</span><br />
  1733. <span class="pytext">vdt</span> <span class="pyoperator">=</span> <span class="pytext">Validator</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  1734. <span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pystring">'default.ini'</span><span class="pyoperator">)</span><br />
  1735. <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">filename</span> <span class="pyoperator">=</span> <span class="pystring">'new_default.ini'</span><br />
  1736. <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">vdt</span><span class="pyoperator">,</span> <span class="pytext">copy</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><br />
  1737. <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div></div>
  1738. <div class="section" id="validation-and-interpolation">
  1739. <h2><a class="toc-backref" href="#id78">9.6   Validation and Interpolation</a></h2>
  1740. <p>String interpolation and validation don't play well together. When validation
  1741. changes type it sets the value. If the value uses interpolation, then the
  1742. interpolation reference would normally be overwritten. Calling <tt class="docutils literal"><span class="pre">write</span></tt> would
  1743. then use the absolute value and the interpolation reference would be lost.</p>
  1744. <p>As a compromise - if the value is unchanged by validation then it is not reset.
  1745. This means strings that pass through validation unmodified will not be
  1746. overwritten. If validation changes type - the value has to be overwritten, and
  1747. any interpolation references are lost  <img src="/smilies/sad.gif" alt="Sad" height="15" width="15" /> .</p>
  1748. </div>
  1749. <div class="section" id="simpleval">
  1750. <h2><a class="toc-backref" href="#id79">9.7   SimpleVal</a></h2>
  1751. <p>You may not need a full validation process, but still want to check if all the
  1752. expected values are present.</p>
  1753. <p>Provided as part of the ConfigObj module is the <tt class="docutils literal"><span class="pre">SimpleVal</span></tt> object. This has
  1754. a dummy <tt class="docutils literal"><span class="pre">test</span></tt> method that always passes.</p>
  1755. <p>The only reason a test will fail is if the value is missing. The return value
  1756. from <tt class="docutils literal"><span class="pre">validate</span></tt> will either be <tt class="docutils literal"><span class="pre">True</span></tt>, meaning all present, or a dictionary
  1757. with <tt class="docutils literal"><span class="pre">False</span></tt> for all missing values/sections.</p>
  1758. <p>To use it, you still need to pass in a valid configspec when you create the
  1759. ConfigObj, but just set all the values to <tt class="docutils literal"><span class="pre">''</span></tt>. Then create an instance of
  1760. <tt class="docutils literal"><span class="pre">SimpleVal</span></tt> and pass it to the <tt class="docutils literal"><span class="pre">validate</span></tt> method.</p>
  1761. <p>As a trivial example if you had the following config file :</p>
  1762. <pre class="literal-block">
  1763. # config file for an application
  1764. port = 80
  1765. protocol = http
  1766. domain = voidspace
  1767. top_level_domain = org.uk
  1768. </pre>
  1769. <p>You would write the following configspec :</p>
  1770. <pre class="literal-block">
  1771. port = ''
  1772. protocol = ''
  1773. domain = ''
  1774. top_level_domain = ''
  1775. </pre>
  1776. <div class="pysrc"><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">Configobj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">configspec</span><span class="pyoperator">)</span><br />
  1777. <span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">SimpleVal</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  1778. <span class="pytext">test</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">)</span><br />
  1779. <span class="pykeyword">if</span> <span class="pytext">test</span> <span class="pyoperator">==</span> <span class="pytext">True</span><span class="pyoperator">:</span><br />
  1780.     <span class="pykeyword">print</span> <span class="pystring">'All values present.'</span><br />
  1781. <span class="pykeyword">elif</span> <span class="pytext">test</span> <span class="pyoperator">==</span> <span class="pytext">False</span><span class="pyoperator">:</span><br />
  1782.     <span class="pykeyword">print</span> <span class="pystring">'No values present!'</span><br />
  1783. <span class="pykeyword">else</span><span class="pyoperator">:</span><br />
  1784.     <span class="pykeyword">for</span> <span class="pytext">entry</span> <span class="pykeyword">in</span> <span class="pytext">test</span><span class="pyoperator">:</span><br />
  1785.         <span class="pykeyword">if</span> <span class="pytext">test</span><span class="pyoperator">[</span><span class="pytext">entry</span><span class="pyoperator">]</span> <span class="pyoperator">==</span> <span class="pytext">False</span><span class="pyoperator">:</span><br />
  1786.             <span class="pykeyword">print</span> <span class="pystring">'"%s" missing.'</span> <span class="pyoperator">%</span> <span class="pytext">entry</span><span class="pytext"></span></div></div>
  1787. </div>
  1788. <div class="section" id="empty-values">
  1789. <h1><a class="toc-backref" href="#id80">10   Empty values</a></h1>
  1790. <p>Many config files from other applications allow empty values. As of version
  1791. 4.3.0, ConfigObj will read these as an empty string.</p>
  1792. <p>A new option/attribute has been added (<tt class="docutils literal"><span class="pre">write_empty_values</span></tt>) to allow
  1793. ConfigObj to write empty strings as empty values.</p>
  1794. <div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
  1795. <span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pystring">'''<br />
  1796.     key =<br />
  1797.     key2 = # a comment<br />
  1798. '''</span><span class="pyoperator">.</span><span class="pytext">splitlines</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  1799. <span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">cfg</span><span class="pyoperator">)</span><br />
  1800. <span class="pykeyword">print</span> <span class="pytext">config</span><br />
  1801. <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">{</span><span class="pystring">'key'</span><span class="pyoperator">:</span> <span class="pystring">''</span><span class="pyoperator">,</span> <span class="pystring">'key2'</span><span class="pyoperator">:</span> <span class="pystring">''</span><span class="pyoperator">}</span><span class="pyoperator">)</span><br />
  1802. <br />
  1803. <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write_empty_values</span> <span class="pyoperator">=</span> <span class="pytext">True</span><br />
  1804. <span class="pykeyword">for</span> <span class="pytext">line</span> <span class="pykeyword">in</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1805.     <span class="pykeyword">print</span> <span class="pytext">line</span><br />
  1806. <br />
  1807. <span class="pytext">key</span> <span class="pyoperator">=</span><br />
  1808. <span class="pytext">key2</span> <span class="pyoperator">=</span>     <span class="pycomment"># a comment</span><span class="pytext"></span></div></div>
  1809. <div class="section" id="unrepr-mode">
  1810. <h1><a class="toc-backref" href="#id81">11   unrepr mode</a></h1>
  1811. <p>The <tt class="docutils literal"><span class="pre">unrepr</span></tt> option allows you to store and retrieve the basic Python
  1812. data-types using config files. It has to use a slightly different syntax to
  1813. normal ConfigObj files. Unsurprisingly it uses Python syntax.</p>
  1814. <p>This means that lists are different (they are surrounded by square brackets),
  1815. and strings <em>must</em> be quoted.</p>
  1816. <p>The types that <tt class="docutils literal"><span class="pre">unrepr</span></tt> can work with are :</p>
  1817. <blockquote>
  1818. <div class="line-block">
  1819. <div class="line">strings, lists tuples</div>
  1820. <div class="line">None, True, False</div>
  1821. <div class="line">dictionaries, integers, floats</div>
  1822. <div class="line">longs and complex numbers</div>
  1823. </div>
  1824. </blockquote>
  1825. <p>You can't store classes, types or instances.</p>
  1826. <p><tt class="docutils literal"><span class="pre">unrepr</span></tt> uses <tt class="docutils literal"><span class="pre">repr(object)</span></tt> to write out values, so it currently <em>doesn't</em>
  1827. check that you are writing valid objects. If you attempt to read an unsupported
  1828. value, ConfigObj will raise a <tt class="docutils literal"><span class="pre">configobj.UnknownType</span></tt> exception.</p>
  1829. <p>Values that are triple quoted cased. The triple quotes are removed <em>before</em>
  1830. converting. This means that you can use triple quotes to write dictionaries
  1831. over several lines in your config files. They won't be written like this
  1832. though.</p>
  1833. <p>If you are writing config files by hand, for use with <tt class="docutils literal"><span class="pre">unrepr</span></tt>, you should
  1834. be aware of the following differences from normal ConfigObj syntax :</p>
  1835. <blockquote>
  1836. <div class="line-block">
  1837. <div class="line">List : <tt class="docutils literal"><span class="pre">['A</span> <span class="pre">List',</span> <span class="pre">'With',</span> <span class="pre">'Strings']</span></tt></div>
  1838. <div class="line">Strings : <tt class="docutils literal"><span class="pre">"Must</span> <span class="pre">be</span> <span class="pre">quoted."</span></tt></div>
  1839. <div class="line">Backslash : <tt class="docutils literal"><span class="pre">"The</span> <span class="pre">backslash</span> <span class="pre">must</span> <span class="pre">be</span> <span class="pre">escaped</span> <span class="pre">\\"</span></tt></div>
  1840. </div>
  1841. </blockquote>
  1842. <p>These all follow normal Python syntax.</p>
  1843. <p>In unrepr mode <em>inline comments</em> are not saved. This is because lines are
  1844. parsed using the <a class="reference external" href="http://docs.python.org/lib/compiler.html">compiler package</a>
  1845. which discards comments.</p>
  1846. </div>
  1847. <div class="section" id="string-interpolation">
  1848. <h1><a class="toc-backref" href="#id82">12   String Interpolation</a></h1>
  1849. <p>ConfigObj allows string interpolation <em>similar</em> to the way <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>
  1850. or <tt class="docutils literal"><span class="pre">string.Template</span></tt> work. The value of the <tt class="docutils literal"><span class="pre">interpolation</span></tt> attribute
  1851. determines which style of interpolation you want to use. Valid values are
  1852. "ConfigParser" or "Template" (case-insensitive, so "configparser" and
  1853. "template" will also work). For backwards compatibility reasons, the value
  1854. <tt class="docutils literal"><span class="pre">True</span></tt> is also a valid value for the <tt class="docutils literal"><span class="pre">interpolation</span></tt> attribute, and
  1855. will select <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>-style interpolation. At some undetermined point
  1856. in the future, that default <em>may</em> change to <tt class="docutils literal"><span class="pre">Template</span></tt>-style interpolation.</p>
  1857. <p>For <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>-style interpolation, you specify a value to be
  1858. substituted by including <tt class="docutils literal"><span class="pre">%(name)s</span></tt> in the value.</p>
  1859. <p>For <tt class="docutils literal"><span class="pre">Template</span></tt>-style interpolation, you specify a value to be substituted
  1860. by including <tt class="docutils literal"><span class="pre">${name}</span></tt> in the value. Alternately, if 'name' is a valid
  1861. Python identifier (i.e., is composed of nothing but alphanumeric characters,
  1862. plus the underscore character), then the braces are optional and the value
  1863. can be written as <tt class="docutils literal"><span class="pre">$name</span></tt>.</p>
  1864. <p>Note that <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>-style interpolation and <tt class="docutils literal"><span class="pre">Template</span></tt>-style
  1865. interpolation are mutually exclusive; you cannot have a configuration file
  1866. that's a mix of one or the other. Pick one and stick to it. <tt class="docutils literal"><span class="pre">Template</span></tt>-style
  1867. interpolation is simpler to read and write by hand, and is recommended if
  1868. you don't have a particular reason to use <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>-style.</p>
  1869. <p>Interpolation checks first the current section to see if <tt class="docutils literal"><span class="pre">name</span></tt> is the key
  1870. to a value. ('name' is case sensitive).</p>
  1871. <p>If it doesn't find it, next it checks the 'DEFAULT' sub-section of the current
  1872. section.</p>
  1873. <p>If it still doesn't find it, it moves on to check the parent section and the
  1874. parent section's 'DEFAULT' subsection, and so on all the way up to the main
  1875. section.</p>
  1876. <p>If the value specified isn't found in any of these locations, then a
  1877. <tt class="docutils literal"><span class="pre">MissingInterpolationOption</span></tt> error is raised (a subclass of
  1878. <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>).</p>
  1879. <p>If it is found then the returned value is also checked for substitutions. This
  1880. allows you to make up compound values (for example directory paths) that use
  1881. more than one default value. It also means it's possible to create circular
  1882. references. If there are any circular references which would cause an infinite
  1883. interpolation loop, an <tt class="docutils literal"><span class="pre">InterpolationLoopError</span></tt> is raised.</p>
  1884. <p>Both of these errors are subclasses of <tt class="docutils literal"><span class="pre">InterpolationError</span></tt>, which is a
  1885. subclass of <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>.</p>
  1886. <p>String interpolation and validation don't play well together. This is because
  1887. validation overwrites values - and so may erase the interpolation references.
  1888. See <a class="reference internal" href="#validation-and-interpolation">Validation and Interpolation</a>. (This can only happen if validation
  1889. has to <em>change</em> the value).</p>
  1890. </div>
  1891. <div class="section" id="comments">
  1892. <h1><a class="toc-backref" href="#id83">13   Comments</a></h1>
  1893. <p>Any line that starts with a '#', possibly preceded by whitespace, is a comment.</p>
  1894. <p>If a config file starts with comments then these are preserved as the
  1895. <a class="reference internal" href="#initial-comment">initial_comment</a>.</p>
  1896. <p>If a config file ends with comments then these are preserved as the
  1897. <a class="reference internal" href="#final-comment">final_comment</a>.</p>
  1898. <p>Every key or section marker may have lines of comments immediately above it.
  1899. These are saved as the <tt class="docutils literal"><span class="pre">comments</span></tt> attribute of the section. Each member is a
  1900. list of lines.</p>
  1901. <p>You can also have a comment inline with a value. These are saved as the
  1902. <tt class="docutils literal"><span class="pre">inline_comments</span></tt> attribute of the section, with one entry per member of the
  1903. section.</p>
  1904. <p>Subsections (section markers in the config file) can also have comments.</p>
  1905. <p>See <a class="reference internal" href="#section-attributes">Section Attributes</a> for more on these attributes.</p>
  1906. <p>These comments are all written back out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.</p>
  1907. </div>
  1908. <div class="section" id="flatten-errors">
  1909. <h1><a class="toc-backref" href="#id84">14   flatten_errors</a></h1>
  1910. <pre class="literal-block">
  1911. flatten_errors(cfg, res)
  1912. </pre>
  1913. <p><a class="reference internal" href="#validation">Validation</a> is a powerful way of checking that the values supplied by the user
  1914. make sense.</p>
  1915. <p>The <a class="reference internal" href="#validate">validate</a> method returns a results dictionary that represents pass or fail
  1916. for each value. This doesn't give you any information about <em>why</em> the check
  1917. failed.</p>
  1918. <p><tt class="docutils literal"><span class="pre">flatten_errors</span></tt> is an example function that turns a results dictionary into
  1919. a flat list, that only contains values that <em>failed</em>.</p>
  1920. <p><tt class="docutils literal"><span class="pre">cfg</span></tt> is the ConfigObj instance being checked, <tt class="docutils literal"><span class="pre">res</span></tt> is the results
  1921. dictionary returned by <tt class="docutils literal"><span class="pre">validate</span></tt>.</p>
  1922. <p>It returns a list of keys that failed. Each member of the list is a tuple :</p>
  1923. <pre class="literal-block">
  1924. ([list of sections...], key, result)
  1925. </pre>
  1926. <p>If <tt class="docutils literal"><span class="pre">validate</span></tt> was called with <tt class="docutils literal"><span class="pre">preserve_errors=False</span></tt> (the default)
  1927. then <tt class="docutils literal"><span class="pre">result</span></tt> will always be <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
  1928. <p><em>list of sections</em> is a flattened list of sections that the key was found
  1929. in.</p>
  1930. <p>If the section was missing then key will be <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
  1931. <p>If the value (or section) was missing then <tt class="docutils literal"><span class="pre">result</span></tt> will be <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
  1932. <p>If <tt class="docutils literal"><span class="pre">validate</span></tt> was called with <tt class="docutils literal"><span class="pre">preserve_errors=True</span></tt> and a value
  1933. was present, but failed the check, then <tt class="docutils literal"><span class="pre">result</span></tt> will be the exception
  1934. object returned. You can use this as a string that describes the failure.</p>
  1935. <p>For example :</p>
  1936. <blockquote>
  1937. <em>The value "3" is of the wrong type</em>.</blockquote>
  1938. <div class="section" id="example-usage">
  1939. <h2><a class="toc-backref" href="#id85">14.1   Example Usage</a></h2>
  1940. <p>The output from <tt class="docutils literal"><span class="pre">flatten_errors</span></tt> is a list of tuples.</p>
  1941. <p>Here is an example of how you could present this information to the user.</p>
  1942. <div class="pysrc"><span class="pytext">vtor</span> <span class="pyoperator">=</span> <span class="pytext">validate</span><span class="pyoperator">.</span><span class="pytext">Validator</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
  1943. <span class="pycomment"># ini is your config file - cs is the configspec<br />
  1944. </span><span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">ini</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">cs</span><span class="pyoperator">)</span><br />
  1945. <span class="pytext">res</span> <span class="pyoperator">=</span> <span class="pytext">cfg</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">vtor</span><span class="pyoperator">,</span> <span class="pytext">preserve_errors</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><br />
  1946. <span class="pykeyword">for</span> <span class="pytext">entry</span> <span class="pykeyword">in</span> <span class="pytext">flatten_errors</span><span class="pyoperator">(</span><span class="pytext">cfg</span><span class="pyoperator">,</span> <span class="pytext">res</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
  1947.     <span class="pycomment"># each entry is a tuple<br />
  1948. </span>    <span class="pytext">section_list</span><span class="pyoperator">,</span> <span class="pytext">key</span><span class="pyoperator">,</span> <span class="pytext">error</span> <span class="pyoperator">=</span> <span class="pytext">entry</span><br />
  1949.     <span class="pykeyword">if</span> <span class="pytext">key</span> <span class="pykeyword">is</span> <span class="pykeyword">not</span> <span class="pytext">None</span><span class="pyoperator">:</span><br />
  1950.        <span class="pytext">section_list</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">key</span><span class="pyoperator">)</span><br />
  1951.     <span class="pykeyword">else</span><span class="pyoperator">:</span><br />
  1952.         <span class="pytext">section_list</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pystring">'[missing section]'</span><span class="pyoperator">)</span><br />
  1953.     <span class="pytext">section_string</span> <span class="pyoperator">=</span> <span class="pystring">', '</span><span class="pyoperator">.</span><span class="pytext">join</span><span class="pyoperator">(</span><span class="pytext">section_list</span><span class="pyoperator">)</span><br />
  1954.     <span class="pykeyword">if</span> <span class="pytext">error</span> <span class="pyoperator">==</span> <span class="pytext">False</span><span class="pyoperator">:</span><br />
  1955.         <span class="pytext">error</span> <span class="pyoperator">=</span> <span class="pystring">'Missing value or section.'</span><br />
  1956.     <span class="pykeyword">print</span> <span class="pytext">section_string</span><span class="pyoperator">,</span> <span class="pystring">' = '</span><span class="pyoperator">,</span> <span class="pytext">error</span><span class="pytext"></span></div></div>
  1957. </div>
  1958. <div class="section" id="configobj-3">
  1959. <h1><a class="toc-backref" href="#id86">15   ConfigObj 3</a></h1>
  1960. <p>ConfigObj 3 is now deprecated in favour of ConfigObj 4. I can fix bugs in
  1961. ConfigObj 3 if needed, though.</p>
  1962. <p>For anyone who still needs it, you can download it here: <a class="reference external" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj3.zip">ConfigObj 3.3.1</a></p>
  1963. <p>You can read the old docs at : <a class="reference external" href="http://www.voidspace.org.uk/python/configobj3.html">ConfigObj 3 Docs</a></p>
  1964. </div>
  1965. <div class="section" id="credits">
  1966. <h1><a class="toc-backref" href="#id87">16   CREDITS</a></h1>
  1967. <p>ConfigObj 4 is written by (and copyright) <a class="reference external" href="http://www.voidspace.org.uk/python/weblog/index.shtml">Michael Foord</a> and
  1968. <a class="reference external" href="http://www.teknico.net">Nicola Larosa</a>.</p>
  1969. <p>Particularly thanks to Nicola Larosa for help on the config file spec, the
  1970. validation system and the doctests.</p>
  1971. <p><em>validate.py</em> was originally written by Michael Foord and Mark Andrews.</p>
  1972. <p>Thanks to others for input and bugfixes.</p>
  1973. </div>
  1974. <div class="section" id="license">
  1975. <h1><a class="toc-backref" href="#id88">17   LICENSE</a></h1>
  1976. <p>ConfigObj, and related files, are licensed under the BSD license. This is a
  1977. very unrestrictive license, but it comes with the usual disclaimer. This is
  1978. free software: test it, break it, just don't blame us if it eats your data !
  1979. Of course if it does, let us know and we'll fix the problem so it doesn't
  1980. happen to anyone else  <img src="/smilies/smile.gif" alt="Smile" height="15" width="15" /> .</p>
  1981. <pre class="literal-block">
  1982. Copyright (c) 2004 - 2008, Michael Foord & Nicola Larosa
  1983. All rights reserved.
  1984.  
  1985. Redistribution and use in source and binary forms, with or without
  1986. modification, are permitted provided that the following conditions are
  1987. met:
  1988.  
  1989.  
  1990.     * Redistributions of source code must retain the above copyright
  1991.       notice, this list of conditions and the following disclaimer.
  1992.  
  1993.     * Redistributions in binary form must reproduce the above
  1994.       copyright notice, this list of conditions and the following
  1995.       disclaimer in the documentation and/or other materials provided
  1996.       with the distribution.
  1997.  
  1998.     * Neither the name of Michael Foord nor Nicola Larosa
  1999.       may be used to endorse or promote products derived from this
  2000.       software without specific prior written permission.
  2001.  
  2002. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2003. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2004. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2005. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2006. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2007. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2008. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2009. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2010. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2011. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2012. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2013. </pre>
  2014. <p>You should also be able to find a copy of this license at : <a class="reference external" href="http://www.voidspace.org.uk/python/license.shtml">BSD License</a></p>
  2015. </div>
  2016. <div class="section" id="todo">
  2017. <h1><a class="toc-backref" href="#id89">18   TODO</a></h1>
  2018. <p>Better support for configuration from multiple files, including tracking
  2019. <em>where</em> the original file came from and writing changes to the correct
  2020. file.</p>
  2021. <p>Make <tt class="docutils literal"><span class="pre">newline</span></tt> an option (as well as an attribute) ?</p>
  2022. <p><tt class="docutils literal"><span class="pre">UTF16</span></tt> encoded files, when returned as a list of lines, will have the
  2023. BOM at the start of every line. Should this be removed from all but the
  2024. first line ?</p>
  2025. <p>Option to set warning type for unicode decode ? (Defaults to strict).</p>
  2026. <p>A method to optionally remove uniform indentation from multiline values.
  2027. (do as an example of using <tt class="docutils literal"><span class="pre">walk</span></tt> - along with string-escape)</p>
  2028. <p>Should the results dictionary from validate be an ordered dictionary if
  2029. <a class="reference external" href="http://www.voidspace.org.uk/python/odict.html">odict</a> is available ?</p>
  2030. <p>Implement some of the sequence methods (which include slicing) from the
  2031. newer <tt class="docutils literal"><span class="pre">odict</span></tt> ?</p>
  2032. <p>Preserve line numbers of values (and possibly the original text of each value).</p>
  2033. </div>
  2034. <div class="section" id="issues">
  2035. <h1><a class="toc-backref" href="#id90">19   ISSUES</a></h1>
  2036. <div class="note">
  2037. <p class="first admonition-title">Note</p>
  2038. <p class="last">Please file any bug reports to <a class="reference external" href="http://www.voidspace.org.uk/python/weblog/index.shtml">Michael Foord</a> or the <strong>ConfigObj</strong>
  2039. <a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a>.</p>
  2040. </div>
  2041. <p>There is currently no way to specify the encoding of a configspec file.</p>
  2042. <p>When using <tt class="docutils literal"><span class="pre">copy</span></tt> mode for validation, it won't copy <tt class="docutils literal"><span class="pre">DEFAULT</span></tt>
  2043. sections. This is so that you <em>can</em> use interpolation in configspec
  2044. files.</p>
  2045. <p><tt class="docutils literal"><span class="pre">validate</span></tt> doesn't report <em>extra</em> values or sections.</p>
  2046. <p>You can't have a keyword with the same name as a section (in the same
  2047. section). They are both dictionary keys - so they would overlap.</p>
  2048. <p>ConfigObj doesn't quote and unquote values if <tt class="docutils literal"><span class="pre">list_values=False</span></tt>.
  2049. This means that leading or trailing whitespace in values will be lost when
  2050. writing. (Unless you manually quote).</p>
  2051. <p>Interpolation checks first the current section, then the 'DEFAULT' subsection
  2052. of the current section, before moving on to the current section's parent and
  2053. so on up the tree.</p>
  2054. <p>Does it matter that we don't support the ':' divider, which is supported
  2055. by <tt class="docutils literal"><span class="pre">ConfigParser</span></tt> ?</p>
  2056. <p>String interpolation and validation don't play well together. When
  2057. validation changes type it sets the value. This will correctly fetch the
  2058. value using interpolation - but then overwrite the interpolation reference.
  2059. If the value is unchanged by validation (it's a string) - but other types
  2060. will be.</p>
  2061. </div>
  2062. <div class="section" id="changelog">
  2063. <h1><a class="toc-backref" href="#id91">20   CHANGELOG</a></h1>
  2064. <p>This is an abbreviated changelog showing the major releases up to version 4.
  2065. From version 4 it lists all releases and changes.</p>
  2066. <div class="section" id="version-4-5-3">
  2067. <h2><a class="toc-backref" href="#id92">20.1   2008/06/27 - Version 4.5.3</a></h2>
  2068. <p>BUGFIX: fixed a problem with <tt class="docutils literal"><span class="pre">copy=True</span></tt> when validating with configspecs that use
  2069. <tt class="docutils literal"><span class="pre">__many__</span></tt> sections.</p>
  2070. </div>
  2071. <div class="section" id="version-4-5-2">
  2072. <h2><a class="toc-backref" href="#id93">20.2   2008/02/05 - Version 4.5.2</a></h2>
  2073. <p>Distribution updated to include version 0.3.2 of <a class="reference internal" href="#validate">validate</a>. This means that
  2074. <tt class="docutils literal"><span class="pre">None</span></tt> as a default value win configspecs works.</p>
  2075. </div>
  2076. <div class="section" id="version-4-5-1">
  2077. <h2><a class="toc-backref" href="#id94">20.3   2008/02/05 - Version 4.5.1</a></h2>
  2078. <p>Distribution updated to include version 0.3.1 of <a class="reference internal" href="#validate">validate</a>. This means that
  2079. Unicode configspecs now work.</p>
  2080. </div>
  2081. <div class="section" id="version-4-5-0">
  2082. <h2><a class="toc-backref" href="#id95">20.4   2008/02/05 - Version 4.5.0</a></h2>
  2083. <p>ConfigObj will now guarantee that files will be written terminated with a
  2084. newline.</p>
  2085. <p>ConfigObj will no longer attempt to import the <tt class="docutils literal"><span class="pre">validate</span></tt> module, until/unless
  2086. you call <tt class="docutils literal"><span class="pre">ConfigObj.validate</span></tt> with <tt class="docutils literal"><span class="pre">preserve_errors=True</span></tt>. This makes it
  2087. faster to import.</p>
  2088. <p>New methods <tt class="docutils literal"><span class="pre">restore_default</span></tt> and <tt class="docutils literal"><span class="pre">restore_defaults</span></tt>. <tt class="docutils literal"><span class="pre">restore_default</span></tt>
  2089. resets an entry to its default value (and returns that value). <tt class="docutils literal"><span class="pre">restore_defaults</span></tt>
  2090. resets all entries to their default value. It doesn't modify entries without a
  2091. default value. You must have validated a ConfigObj (which populates the
  2092. <tt class="docutils literal"><span class="pre">default_values</span></tt> dictionary) before calling these methods.</p>
  2093. <p>BUGFIX: Proper quoting of keys, values and list values that contain hashes
  2094. (when writing).  When <tt class="docutils literal"><span class="pre">list_values=False</span></tt>, values containing hashes are
  2095. triple quoted.</p>
  2096. <p>Added the <tt class="docutils literal"><span class="pre">reload</span></tt> method. This reloads a ConfigObj from file. If the filename
  2097. attribute is not set then a <tt class="docutils literal"><span class="pre">ReloadError</span></tt> (a new exception inheriting from
  2098. <tt class="docutils literal"><span class="pre">IOError</span></tt>) is raised.</p>
  2099. <p>BUGFIX: Files are read in with 'rb' mode, so that native/non-native line endings work!</p>
  2100. <p>Minor efficiency improvement in <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode.</p>
  2101. <p>Added missing docstrings for some overidden dictionary methods.</p>
  2102. <p>Added the <tt class="docutils literal"><span class="pre">reset</span></tt> method. This restores a ConfigObj to a freshly created state.</p>
  2103. <p>Removed old CHANGELOG file.</p>
  2104. </div>
  2105. <div class="section" id="version-4-4-0">
  2106. <h2><a class="toc-backref" href="#id96">20.5   2007/02/04 - Version 4.4.0</a></h2>
  2107. <p>Official release of 4.4.0</p>
  2108. </div>
  2109. <div class="section" id="version-4-3-3-alpha4">
  2110. <h2><a class="toc-backref" href="#id97">20.6   2006/12/17 - Version 4.3.3-alpha4</a></h2>
  2111. <p>By Nicola Larosa</p>
  2112. <p>Allowed arbitrary indentation in the <tt class="docutils literal"><span class="pre">indent_type</span></tt> parameter, removed the
  2113. <tt class="docutils literal"><span class="pre">NUM_INDENT_SPACES</span></tt> and <tt class="docutils literal"><span class="pre">MAX_INTERPOL_DEPTH</span></tt> (a leftover) constants,
  2114. added indentation tests (including another docutils workaround, sigh), updated
  2115. the documentation.</p>
  2116. <p>By Michael Foord</p>
  2117. <p>Made the import of <tt class="docutils literal"><span class="pre">compiler</span></tt> conditional so that <tt class="docutils literal"><span class="pre">ConfigObj</span></tt> can be used
  2118. with <a class="reference external" href="http://www.codeplex.com/IronPython">IronPython</a>.</p>
  2119. </div>
  2120. <div class="section" id="version-4-3-3-alpha3">
  2121. <h2><a class="toc-backref" href="#id98">20.7   2006/12/17 - Version 4.3.3-alpha3</a></h2>
  2122. <p>By Nicola Larosa</p>
  2123. <p>Added a missing <tt class="docutils literal"><span class="pre">self.</span></tt> in the _handle_comment method and a related test,
  2124. per Sourceforge bug #1523975.</p>
  2125. </div>
  2126. <div class="section" id="version-4-3-3-alpha2">
  2127. <h2><a class="toc-backref" href="#id99">20.8   2006/12/09 - Version 4.3.3-alpha2</a></h2>
  2128. <p>By Nicola Larosa</p>
  2129. <p>Changed interpolation search strategy, based on this patch by Robin Munn:
  2130. <a class="reference external" href="http://sourceforge.net/mailarchive/message.php?msg_id=17125993">http://sourceforge.net/mailarchive/message.php?msg_id=17125993</a></p>
  2131. </div>
  2132. <div class="section" id="version-4-3-3-alpha1">
  2133. <h2><a class="toc-backref" href="#id100">20.9   2006/12/09 - Version 4.3.3-alpha1</a></h2>
  2134. <p>By Nicola Larosa</p>
  2135. <p>Added Template-style interpolation, with tests, based on this patch by
  2136. Robin Munn: <a class="reference external" href="http://sourceforge.net/mailarchive/message.php?msg_id=17125991">http://sourceforge.net/mailarchive/message.php?msg_id=17125991</a>
  2137. (awful archives, bad Sourceforge, bad).</p>
  2138. </div>
  2139. <div class="section" id="version-4-3-2">
  2140. <h2><a class="toc-backref" href="#id101">20.10   2006/06/04 - Version 4.3.2</a></h2>
  2141. <p>Changed error handling, if parsing finds a single error then that error will
  2142. be re-raised. That error will still have an <tt class="docutils literal"><span class="pre">errors</span></tt> and a <tt class="docutils literal"><span class="pre">config</span></tt>
  2143. attribute.</p>
  2144. <p>Fixed bug where '\n' terminated files could be truncated.</p>
  2145. <p>Bugfix in <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode, it couldn't handle '#' in values. (Thanks to
  2146. Philippe Normand for the report.)</p>
  2147. <p>As a consequence of this fix, ConfigObj doesn't now keep inline comments in
  2148. <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode. This is because the parser in the <a class="reference external" href="http://docs.python.org/lib/compiler.html">compiler package</a>
  2149. doesn't keep comments.  <img src="/smilies/smile.gif" alt="Smile" height="15" width="15" /> </p>
  2150. <p>Error messages are now more useful. They tell you the number of parsing errors
  2151. and the line number of the first error. (In the case of multiple errors.)</p>
  2152. <p>Line numbers in exceptions now start at 1, not 0.</p>
  2153. <p>Errors in <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode are now handled the same way as in the normal mode.
  2154. The errors stored will be an <tt class="docutils literal"><span class="pre">UnreprError</span></tt>.</p>
  2155. </div>
  2156. <div class="section" id="version-4-3-1">
  2157. <h2><a class="toc-backref" href="#id102">20.11   2006/04/29 - Version 4.3.1</a></h2>
  2158. <p>Added <tt class="docutils literal"><span class="pre">validate.py</span></tt> back into <tt class="docutils literal"><span class="pre">configobj.zip</span></tt>. (Thanks to Stewart
  2159. Midwinter)</p>
  2160. <p>Updated to <a class="reference external" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py">validate.py</a> 0.2.2.</p>
  2161. <p>Preserve tuples when calling the <tt class="docutils literal"><span class="pre">dict</span></tt> method. (Thanks to Gustavo Niemeyer.)</p>
  2162. <p>Changed <tt class="docutils literal"><span class="pre">__repr__</span></tt> to return a string that contains <tt class="docutils literal"><span class="pre">ConfigObj({</span> <span class="pre">...</span> <span class="pre">})</span></tt>.</p>
  2163. <p>Change so that an options dictionary isn't modified by passing it to ConfigObj.
  2164. (Thanks to Artarious.)</p>
  2165. <p>Added ability to handle negative integers in <tt class="docutils literal"><span class="pre">unrepr</span></tt>. (Thanks to Kevin
  2166. Dangoor.)</p>
  2167. </div>
  2168. <div class="section" id="version-4-3-0">
  2169. <h2><a class="toc-backref" href="#id103">20.12   2006/03/24 - Version 4.3.0</a></h2>
  2170. <p>Moved the tests and the CHANGELOG (etc) into a separate file. This has reduced
  2171. the size of <tt class="docutils literal"><span class="pre">configobj.py</span></tt> by about 40%.</p>
  2172. <p>Added the <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode to reading and writing config files. Thanks to Kevin
  2173. Dangoor for this suggestion.</p>
  2174. <p>Empty values are now valid syntax. They are read as an empty string <tt class="docutils literal"><span class="pre">''</span></tt>.
  2175. (<tt class="docutils literal"><span class="pre">key</span> <span class="pre">=</span></tt>, or <tt class="docutils literal"><span class="pre">key</span> <span class="pre">=</span> <span class="pre">#</span> <span class="pre">comment</span></tt>.)</p>
  2176. <p><tt class="docutils literal"><span class="pre">validate</span></tt> now honours the order of the configspec.</p>
  2177. <p>Added the <tt class="docutils literal"><span class="pre">copy</span></tt> mode to validate. Thanks to Louis Cordier for this
  2178. suggestion.</p>
  2179. <p>Fixed bug where files written on windows could be given <tt class="docutils literal"><span class="pre">'\r\r\n'</span></tt> line
  2180. terminators.</p>
  2181. <p>Fixed bug where last occurring comment line could be interpreted as the
  2182. final comment if the last line isn't terminated.</p>
  2183. <p>Fixed bug where nested list values would be flattened when <tt class="docutils literal"><span class="pre">write</span></tt> is
  2184. called. Now sub-lists have a string representation written instead.</p>
  2185. <p>Deprecated <tt class="docutils literal"><span class="pre">encode</span></tt> and <tt class="docutils literal"><span class="pre">decode</span></tt> methods instead.</p>
  2186. <p>You can now pass in a ConfigObj instance as a configspec (remember to read
  2187. the configspec file using <tt class="docutils literal"><span class="pre">list_values=False</span></tt>).</p>
  2188. <p>Sorted footnotes in the docs.</p>
  2189. </div>
  2190. <div class="section" id="version-4-2-0">
  2191. <h2><a class="toc-backref" href="#id104">20.13   2006/02/16 - Version 4.2.0</a></h2>
  2192. <p>Removed <tt class="docutils literal"><span class="pre">BOM_UTF8</span></tt> from <tt class="docutils literal"><span class="pre">__all__</span></tt>.</p>
  2193. <p>The <tt class="docutils literal"><span class="pre">BOM</span></tt> attribute has become a boolean. (Defaults to <tt class="docutils literal"><span class="pre">False</span></tt>.) It is
  2194. <em>only</em> <tt class="docutils literal"><span class="pre">True</span></tt> for the <tt class="docutils literal"><span class="pre">UTF16/UTF8</span></tt> encodings.</p>
  2195. <p>File like objects no longer need a <tt class="docutils literal"><span class="pre">seek</span></tt> attribute.</p>
  2196. <p>Full unicode support added. New options/attributes <tt class="docutils literal"><span class="pre">encoding</span></tt>,
  2197. <tt class="docutils literal"><span class="pre">default_encoding</span></tt>.</p>
  2198. <p>ConfigObj no longer keeps a reference to file like objects. Instead the
  2199. <tt class="docutils literal"><span class="pre">write</span></tt> method takes a file like object as an optional argument. (Which
  2200. will be used in preference of the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute if that exists as
  2201. well.)</p>
  2202. <p>utf16 files decoded to unicode.</p>
  2203. <p>If <tt class="docutils literal"><span class="pre">BOM</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, but no encoding specified, then the utf8 BOM is
  2204. written out at the start of the file. (It will normally only be <tt class="docutils literal"><span class="pre">True</span></tt> if
  2205. the utf8 BOM was found when the file was read.)</p>
  2206. <p>Thanks to Aaron Bentley for help and testing on the unicode issues.</p>
  2207. <p>File paths are <em>not</em> converted to absolute paths, relative paths will
  2208. remain relative as the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute.</p>
  2209. <p>Fixed bug where <tt class="docutils literal"><span class="pre">final_comment</span></tt> wasn't returned if <tt class="docutils literal"><span class="pre">write</span></tt> is returning
  2210. a list of lines.</p>
  2211. <p>Deprecated <tt class="docutils literal"><span class="pre">istrue</span></tt>, replaced it with <tt class="docutils literal"><span class="pre">as_bool</span></tt>.</p>
  2212. <p>Added <tt class="docutils literal"><span class="pre">as_int</span></tt> and <tt class="docutils literal"><span class="pre">as_float</span></tt>.</p>
  2213. </div>
  2214. <div class="section" id="version-4-1-0">
  2215. <h2><a class="toc-backref" href="#id105">20.14   2005/12/14 - Version 4.1.0</a></h2>
  2216. <p>Added <tt class="docutils literal"><span class="pre">merge</span></tt>, a recursive update.</p>
  2217. <p>Added <tt class="docutils literal"><span class="pre">preserve_errors</span></tt> to <tt class="docutils literal"><span class="pre">validate</span></tt> and the <tt class="docutils literal"><span class="pre">flatten_errors</span></tt>
  2218. example function.</p>
  2219. <p>Thanks to Matthew Brett for suggestions and helping me iron out bugs.</p>
  2220. <p>Fixed bug where a config file is <em>all</em> comment, the comment will now be
  2221. <tt class="docutils literal"><span class="pre">initial_comment</span></tt> rather than <tt class="docutils literal"><span class="pre">final_comment</span></tt>.</p>
  2222. <p>Validation no longer done on the 'DEFAULT' section (only in the root level).
  2223. This allows interpolation in configspecs.</p>
  2224. <p>Also use the new list syntax in <a class="reference internal" href="#validate">validate</a> 0.2.1. (For configspecs).</p>
  2225. </div>
  2226. <div class="section" id="version-4-0-2">
  2227. <h2><a class="toc-backref" href="#id106">20.15   2005/12/02 - Version 4.0.2</a></h2>
  2228. <p>Fixed bug in <tt class="docutils literal"><span class="pre">create_empty</span></tt>. Thanks to Paul Jimenez for the report.</p>
  2229. </div>
  2230. <div class="section" id="version-4-0-1">
  2231. <h2><a class="toc-backref" href="#id107">20.16   2005/11/05 - Version 4.0.1</a></h2>
  2232. <p>Fixed bug in <tt class="docutils literal"><span class="pre">Section.walk</span></tt> when transforming names as well as values.</p>
  2233. <p>Added the <tt class="docutils literal"><span class="pre">istrue</span></tt> method. (Fetches the boolean equivalent of a string
  2234. value).</p>
  2235. <p>Fixed <tt class="docutils literal"><span class="pre">list_values=False</span></tt> - they are now only quoted/unquoted if they
  2236. are multiline values.</p>
  2237. <p>List values are written as <tt class="docutils literal"><span class="pre">item,</span> <span class="pre">item</span></tt> rather than <tt class="docutils literal"><span class="pre">item,item</span></tt>.</p>
  2238. </div>
  2239. <div class="section" id="version-4-0-0">
  2240. <h2><a class="toc-backref" href="#id108">20.17   2005/10/17 - Version 4.0.0</a></h2>
  2241. <p><strong>ConfigObj 4.0.0 Final</strong></p>
  2242. <p>Fixed bug in <tt class="docutils literal"><span class="pre">setdefault</span></tt>. When creating a new section with setdefault the
  2243. reference returned would be to the dictionary passed in <em>not</em> to the new
  2244. section. Bug fixed and behaviour documented.</p>
  2245. <p>Obscure typo/bug fixed in <tt class="docutils literal"><span class="pre">write</span></tt>. Wouldn't have affected anyone though.</p>
  2246. </div>
  2247. <div class="section" id="version-4-0-0-beta-5">
  2248. <h2><a class="toc-backref" href="#id109">20.18   2005/09/09 - Version 4.0.0 beta 5</a></h2>
  2249. <p>Removed <tt class="docutils literal"><span class="pre">PositionError</span></tt>.</p>
  2250. <p>Allowed quotes around keys as documented.</p>
  2251. <p>Fixed bug with commas in comments. (matched as a list value)</p>
  2252. </div>
  2253. <div class="section" id="version-4-0-0-beta-4">
  2254. <h2><a class="toc-backref" href="#id110">20.19   2005/09/07 - Version 4.0.0 beta 4</a></h2>
  2255. <p>Fixed bug in <tt class="docutils literal"><span class="pre">__delitem__</span></tt>. Deleting an item no longer deletes the
  2256. <tt class="docutils literal"><span class="pre">inline_comments</span></tt> attribute.</p>
  2257. <p>Fixed bug in initialising ConfigObj from a ConfigObj.</p>
  2258. <p>Changed the mailing list address.</p>
  2259. </div>
  2260. <div class="section" id="version-4-0-0-beta-3">
  2261. <h2><a class="toc-backref" href="#id111">20.20   2005/08/28 - Version 4.0.0 beta 3</a></h2>
  2262. <p>Interpolation is switched off before writing out files.</p>
  2263. <p>Fixed bug in handling <tt class="docutils literal"><span class="pre">StringIO</span></tt> instances. (Thanks to report from
  2264. Gustavo Niemeyer.)</p>
  2265. <p>Moved the doctests from the <tt class="docutils literal"><span class="pre">__init__</span></tt> method to a separate function.
  2266. (For the sake of IDE calltips).</p>
  2267. </div>
  2268. <div class="section" id="version-4-0-0-beta-2">
  2269. <h2><a class="toc-backref" href="#id112">20.21   2005/08/25 - Version 4.0.0 beta 2</a></h2>
  2270. <p>Amendments to <em>validate.py</em>.</p>
  2271. <p>First public release.</p>
  2272. </div>
  2273. <div class="section" id="version-4-0-0-beta-1">
  2274. <h2><a class="toc-backref" href="#id113">20.22   2005/08/21 - Version 4.0.0 beta 1</a></h2>
  2275. <p>Reads nested subsections to any depth.</p>
  2276. <p>Multiline values.</p>
  2277. <p>Simplified options and methods.</p>
  2278. <p>New list syntax.</p>
  2279. <p>Faster, smaller, and better parser.</p>
  2280. <p>Validation greatly improved. Includes:</p>
  2281. <blockquote>
  2282. <ul class="simple">
  2283. <li>type conversion</li>
  2284. <li>default values</li>
  2285. <li>repeated sections</li>
  2286. </ul>
  2287. </blockquote>
  2288. <p>Improved error handling.</p>
  2289. <p>Plus lots of other improvements.  <img src="/smilies/biggrin.gif" alt="Very Happy" height="15" width="15" /> </p>
  2290. </div>
  2291. <div class="section" id="version-3-0-0">
  2292. <h2><a class="toc-backref" href="#id114">20.23   2004/05/24 - Version 3.0.0</a></h2>
  2293. <p>Several incompatible changes: another major overhaul and change. (Lots of
  2294. improvements though).</p>
  2295. <p>Added support for standard config files with sections. This has an entirely
  2296. new interface: each section is a dictionary of values.</p>
  2297. <p>Changed the update method to be called writein: update clashes with a dict
  2298. method.</p>
  2299. <p>Made various attributes keyword arguments, added several.</p>
  2300. <p>Configspecs and orderlists have changed a great deal.</p>
  2301. <p>Removed support for adding dictionaries: use update instead.</p>
  2302. <p>Now subclasses a new class called caselessDict. This should add various
  2303. dictionary methods that could have caused errors before.</p>
  2304. <p>It also preserves the original casing of keywords when writing them back out.</p>
  2305. <p>Comments are also saved using a <tt class="docutils literal"><span class="pre">caselessDict</span></tt>.</p>
  2306. <p>Using a non-string key will now raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt> rather than converting
  2307. the key.</p>
  2308. <p>Added an exceptions keyword for <em>much</em> better handling of errors.</p>
  2309. <p>Made <tt class="docutils literal"><span class="pre">creatempty=False</span></tt> the default.</p>
  2310. <p>Now checks indict <em>and</em> any keyword args. Keyword args take precedence over
  2311. indict.</p>
  2312. <p><tt class="docutils literal"><span class="pre">'</span> <span class="pre">',</span> <span class="pre">':',</span> <span class="pre">'=',</span> <span class="pre">','</span></tt> and <tt class="docutils literal"><span class="pre">'\t'</span></tt> are now all valid dividers where the
  2313. keyword is unquoted.</p>
  2314. <p>ConfigObj now does no type checking against configspec when you set items.</p>
  2315. <p>delete and add methods removed (they were unnecessary).</p>
  2316. <p>Docs rewritten to include all this gumph and more; actually ConfigObj is
  2317. <em>really</em> easy to use.</p>
  2318. <p>Support for stdout was removed.</p>
  2319. <p>A few new methods added.</p>
  2320. <p>Charmap is now incorporated into ConfigObj.</p>
  2321. </div>
  2322. <div class="section" id="version-2-0-0-beta">
  2323. <h2><a class="toc-backref" href="#id115">20.24   2004/03/14 - Version 2.0.0 beta</a></h2>
  2324. <p>Re-written it to subclass dict. My first forays into inheritance and operator
  2325. overloading.</p>
  2326. <p>The config object now behaves like a dictionary.</p>
  2327. <p>I've completely broken the interface, but I don't think anyone was really
  2328. using it anyway.</p>
  2329. <p>This new version is much more 'classy'.  <img src="/smilies/wink.gif" alt="Wink" height="15" width="15" /> </p>
  2330. <p>It will also read straight from/to a filename and completely parse a config
  2331. file without you <em>having</em> to supply a config spec.</p>
  2332. <p>Uses listparse, so can handle nested list items as values.</p>
  2333. <p>No longer has getval and setval methods: use normal dictionary methods, or add
  2334. and delete.</p>
  2335. </div>
  2336. <div class="section" id="version-1-0-5">
  2337. <h2><a class="toc-backref" href="#id116">20.25   2004/01/29 - Version 1.0.5</a></h2>
  2338. <p>Version 1.0.5 has a couple of bugfixes as well as a couple of useful additions
  2339. over previous versions.</p>
  2340. <p>Since 1.0.0 the buildconfig function has been moved into this distribution,
  2341. and the methods reset, verify, getval and setval have been added.</p>
  2342. <p>A couple of bugs have been fixed.</p>
  2343. </div>
  2344. <div class="section" id="origins">
  2345. <h2><a class="toc-backref" href="#id117">20.26   Origins</a></h2>
  2346. <p>ConfigObj originated in a set of functions for reading config files in the
  2347. <a class="reference external" href="http://www.voidspace.org.uk/atlantibots/">atlantibots</a> project. The original
  2348. functions were written by Rob McNeur.</p>
  2349. </div>
  2350. </div>
  2351. <hr class="docutils" />
  2352. <div class="section" id="footnotes">
  2353. <h1><a class="toc-backref" href="#id118">21   Footnotes</a></h1>
  2354. <table class="docutils footnote" frame="void" id="id16" rules="none">
  2355. <colgroup><col class="label" /><col /></colgroup>
  2356. <tbody valign="top">
  2357. <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>And if you discover any bugs, let us know. We'll fix them quickly.</td></tr>
  2358. </tbody>
  2359. </table>
  2360. <table class="docutils footnote" frame="void" id="id17" rules="none">
  2361. <colgroup><col class="label" /><col /></colgroup>
  2362. <tbody valign="top">
  2363. <tr><td class="label"><a class="fn-backref" href="#id3">[2]</a></td><td>If you specify a filename that doesn't exist, ConfigObj will assume you
  2364. are creating a new one. See the <em>create_empty</em> and <em>file_error</em> <a class="reference internal" href="#options">options</a>.</td></tr>
  2365. </tbody>
  2366. </table>
  2367. <table class="docutils footnote" frame="void" id="id18" rules="none">
  2368. <colgroup><col class="label" /><col /></colgroup>
  2369. <tbody valign="top">
  2370. <tr><td class="label"><a class="fn-backref" href="#id4">[3]</a></td><td>They can be byte strings (<em>ordinary</em> strings) or Unicode.</td></tr>
  2371. </tbody>
  2372. </table>
  2373. <table class="docutils footnote" frame="void" id="id19" rules="none">
  2374. <colgroup><col class="label" /><col /></colgroup>
  2375. <tbody valign="top">
  2376. <tr><td class="label"><a class="fn-backref" href="#id5">[4]</a></td><td>Except we don't support the RFC822 style line continuations, nor ':' as
  2377. a divider.</td></tr>
  2378. </tbody>
  2379. </table>
  2380. <table class="docutils footnote" frame="void" id="id20" rules="none">
  2381. <colgroup><col class="label" /><col /></colgroup>
  2382. <tbody valign="top">
  2383. <tr><td class="label"><a class="fn-backref" href="#id6">[5]</a></td><td>This is a change in ConfigObj 4.2.0. Note that ConfigObj doesn't call
  2384. the seek method of any file like object you pass in. You may want to call
  2385. <tt class="docutils literal"><span class="pre">file_object.seek(0)</span></tt> yourself, first.</td></tr>
  2386. </tbody>
  2387. </table>
  2388. <table class="docutils footnote" frame="void" id="id21" rules="none">
  2389. <colgroup><col class="label" /><col /></colgroup>
  2390. <tbody valign="top">
  2391. <tr><td class="label"><a class="fn-backref" href="#id7">[6]</a></td><td><p class="first">A side effect of this is that it enables you to copy a ConfigObj :</p>
  2392. <div class="pysrc"><span class="pycomment"># only copies members<br />
  2393. </span><span class="pycomment"># not attributes/comments<br />
  2394. </span><span class="pytext">config2</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">config1</span><span class="pyoperator">)</span><span class="pytext"></span></div><p class="last">The order of values and sections will not be preserved, though.</p>
  2395. </td></tr>
  2396. </tbody>
  2397. </table>
  2398. <table class="docutils footnote" frame="void" id="id22" rules="none">
  2399. <colgroup><col class="label" /><col /></colgroup>
  2400. <tbody valign="top">
  2401. <tr><td class="label"><a class="fn-backref" href="#id8">[7]</a></td><td>Other than lists of strings.</td></tr>
  2402. </tbody>
  2403. </table>
  2404. <table class="docutils footnote" frame="void" id="id23" rules="none">
  2405. <colgroup><col class="label" /><col /></colgroup>
  2406. <tbody valign="top">
  2407. <tr><td class="label"><a class="fn-backref" href="#id9">[8]</a></td><td>The exception is if it detects a <tt class="docutils literal"><span class="pre">UTF16</span></tt> encoded file which it
  2408. must decode before parsing.</td></tr>
  2409. </tbody>
  2410. </table>
  2411. <table class="docutils footnote" frame="void" id="id24" rules="none">
  2412. <colgroup><col class="label" /><col /></colgroup>
  2413. <tbody valign="top">
  2414. <tr><td class="label"><a class="fn-backref" href="#id10">[9]</a></td><td>The method signature shows that this method takes
  2415. two arguments. The second is the section to be written. This is because the
  2416. <tt class="docutils literal"><span class="pre">write</span></tt> method is called recursively.</td></tr>
  2417. </tbody>
  2418. </table>
  2419. <table class="docutils footnote" frame="void" id="id25" rules="none">
  2420. <colgroup><col class="label" /><col /></colgroup>
  2421. <tbody valign="top">
  2422. <tr><td class="label"><a class="fn-backref" href="#id11">[10]</a></td><td>The dict method doesn't actually use the deepcopy mechanism. This means
  2423. if you add nested lists (etc) to your ConfigObj, then the dictionary
  2424. returned by dict may contain some references. For all <em>normal</em> ConfigObjs
  2425. it will return a deepcopy.</td></tr>
  2426. </tbody>
  2427. </table>
  2428. <table class="docutils footnote" frame="void" id="id26" rules="none">
  2429. <colgroup><col class="label" /><col /></colgroup>
  2430. <tbody valign="top">
  2431. <tr><td class="label"><a class="fn-backref" href="#id12">[11]</a></td><td>Passing <tt class="docutils literal"><span class="pre">(section,</span> <span class="pre">key)</span></tt> rather than <tt class="docutils literal"><span class="pre">(value,</span> <span class="pre">key)</span></tt> allows you to
  2432. change the value by setting <tt class="docutils literal"><span class="pre">section[key]</span> <span class="pre">=</span> <span class="pre">newval</span></tt>. It also gives you
  2433. access to the <em>rename</em> method of the section.</td></tr>
  2434. </tbody>
  2435. </table>
  2436. <table class="docutils footnote" frame="void" id="id27" rules="none">
  2437. <colgroup><col class="label" /><col /></colgroup>
  2438. <tbody valign="top">
  2439. <tr><td class="label"><a class="fn-backref" href="#id14">[12]</a></td><td>Minimum required version of <em>validate.py</em> 0.2.0 .</td></tr>
  2440. </tbody>
  2441. </table>
  2442. <div class="note">
  2443. <p class="first admonition-title">Note</p>
  2444. <p class="last">Rendering this document with docutils also needs the
  2445. textmacros module and the PySrc CSS stuff. See
  2446. <a class="reference external" href="http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml">http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml</a></p>
  2447. </div>
  2448. <div align="center">
  2449.     <p>
  2450.         <a href="http://www.python.org">
  2451.             <img src="images/new_python.gif" width="100" height="103" border="0"
  2452.                 alt="Powered by Python" />
  2453.         </a>
  2454.         <a href="http://sourceforge.net">
  2455.             <img src="http://sourceforge.net/sflogo.php?group_id=123265&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" />
  2456.         </a>
  2457.         <a href="http://www.opensource.org">
  2458.             <img src="images/osi-certified-120x100.gif" width="120" height="100" border="0"
  2459.                 alt="Certified Open Source"/>
  2460.         </a>
  2461.     </p>
  2462.     <p>
  2463.         <a href="http://www.voidspace.org.uk/python/index.shtml">
  2464.             <img src="images/pythonbanner.gif" width="468" height="60"
  2465.             alt="Python on Voidspace" border="0" />
  2466.         </a>
  2467.     </p>
  2468.  
  2469. </div></div>
  2470. </div>
  2471. <div class="footer">
  2472. <hr class="footer" />
  2473. <a class="reference external" href="configobj.txt">View document source</a>.
  2474. Generated on: 2008-06-28 12:01 UTC.
  2475. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  2476.  
  2477. </div>
  2478. </body>
  2479. </html>
  2480.